asm-tst8.c revision d7f8d0caa7a357f9f4765e5dc93255f5057eba2e
1/* Copyright (C) 2002, 2005 Red Hat, Inc. 2 Written by Ulrich Drepper <drepper@redhat.com>, 2002. 3 4 This program is Open Source software; you can redistribute it and/or 5 modify it under the terms of the Open Software License version 1.0 as 6 published by the Open Source Initiative. 7 8 You should have received a copy of the Open Software License along 9 with this program; if not, you may obtain a copy of the Open Software 10 License version 1.0 from http://www.opensource.org/licenses/osl.php or 11 by writing the Open Source Initiative c/o Lawrence Rosen, Esq., 12 3001 King Ranch Road, Ukiah, CA 95482. */ 13 14#ifdef HAVE_CONFIG_H 15# include <config.h> 16#endif 17 18#include <fcntl.h> 19#include <inttypes.h> 20#include ELFUTILS_HEADER(asm) 21#include <libelf.h> 22#include <stdio.h> 23#include <unistd.h> 24 25 26static const char fname[] = "asm-tst8-out.o"; 27 28 29int 30main (void) 31{ 32 int result = 0; 33 size_t cnt; 34 AsmCtx_t *ctx; 35 Elf *elf; 36 int fd; 37 38 elf_version (EV_CURRENT); 39 40 Ebl *ebl = ebl_openbackend_machine (EM_386); 41 if (ebl == NULL) 42 { 43 puts ("cannot open backend library"); 44 return 1; 45 } 46 47 ctx = asm_begin (fname, ebl, false); 48 if (ctx == NULL) 49 { 50 printf ("cannot create assembler context: %s\n", asm_errmsg (-1)); 51 return 1; 52 } 53 54 if (asm_newabssym (ctx, "tst8-out.s", 4, 0xfeedbeef, STT_FILE, STB_LOCAL) 55 == NULL) 56 { 57 printf ("cannot create absolute symbol: %s\n", asm_errmsg (-1)); 58 asm_abort (ctx); 59 return 1; 60 } 61 62 /* Create the output file. */ 63 if (asm_end (ctx) != 0) 64 { 65 printf ("cannot create output file: %s\n", asm_errmsg (-1)); 66 asm_abort (ctx); 67 return 1; 68 } 69 70 /* Check the file. */ 71 fd = open (fname, O_RDONLY); 72 if (fd == -1) 73 { 74 printf ("cannot open generated file: %m\n"); 75 result = 1; 76 goto out; 77 } 78 79 elf = elf_begin (fd, ELF_C_READ, NULL); 80 if (elf == NULL) 81 { 82 printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1)); 83 result = 1; 84 goto out_close; 85 } 86 if (elf_kind (elf) != ELF_K_ELF) 87 { 88 puts ("not a valid ELF file"); 89 result = 1; 90 goto out_close2; 91 } 92 93 for (cnt = 1; 1; ++cnt) 94 { 95 Elf_Scn *scn; 96 GElf_Shdr shdr_mem; 97 GElf_Shdr *shdr; 98 99 scn = elf_getscn (elf, cnt); 100 if (scn == NULL) 101 { 102 printf ("cannot get section %Zd: %s\n", cnt, elf_errmsg (-1)); 103 result = 1; 104 continue; 105 } 106 107 shdr = gelf_getshdr (scn, &shdr_mem); 108 if (shdr == NULL) 109 { 110 printf ("cannot get section header for section %Zd: %s\n", 111 cnt, elf_errmsg (-1)); 112 result = 1; 113 continue; 114 } 115 /* We are looking for the symbol table. */ 116 if (shdr->sh_type != SHT_SYMTAB) 117 continue; 118 119 for (cnt = 1; cnt< (shdr->sh_size 120 / gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT)); 121 ++cnt) 122 { 123 GElf_Sym sym_mem; 124 GElf_Sym *sym; 125 126 if (cnt > 1) 127 { 128 puts ("too many symbol"); 129 result = 1; 130 break; 131 } 132 133 sym = gelf_getsym (elf_getdata (scn, NULL), cnt, &sym_mem); 134 if (sym == NULL) 135 { 136 printf ("cannot get symbol %zu: %s\n", cnt, elf_errmsg (-1)); 137 result = 1; 138 } 139 else 140 { 141 if (sym->st_shndx != SHN_ABS) 142 { 143 printf ("expected common symbol, got section %u\n", 144 (unsigned int) sym->st_shndx); 145 result = 1; 146 } 147 148 if (sym->st_value != 0xfeedbeef) 149 { 150 printf ("requested value 0xfeedbeef, is %#" PRIxMAX "\n", 151 (uintmax_t) sym->st_value); 152 result = 1; 153 } 154 155 if (sym->st_size != 4) 156 { 157 printf ("requested size 4, is %" PRIuMAX "\n", 158 (uintmax_t) sym->st_value); 159 result = 1; 160 } 161 162 if (GELF_ST_TYPE (sym->st_info) != STT_FILE) 163 { 164 printf ("requested type FILE, is %u\n", 165 (unsigned int) GELF_ST_TYPE (sym->st_info)); 166 result = 1; 167 } 168 } 169 } 170 171 break; 172 } 173 174 out_close2: 175 elf_end (elf); 176 out_close: 177 close (fd); 178 out: 179 /* We don't need the file anymore. */ 180 unlink (fname); 181 182 ebl_closebackend (ebl); 183 184 return result; 185} 186