asm-tst8.c revision a38998e815ccde5d90ff0800c31da255eb3430d3
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#include <fcntl.h> 15#include <inttypes.h> 16#include <libasm.h> 17#include <libelf.h> 18#include <stdio.h> 19#include <unistd.h> 20 21 22static const char fname[] = "asm-tst8-out.o"; 23 24 25int 26main (void) 27{ 28 int result = 0; 29 size_t cnt; 30 AsmCtx_t *ctx; 31 Elf *elf; 32 int fd; 33 34 elf_version (EV_CURRENT); 35 36 Ebl *ebl = ebl_openbackend_machine (EM_386); 37 if (ebl == NULL) 38 { 39 puts ("cannot open backend library"); 40 return 1; 41 } 42 43 ctx = asm_begin (fname, ebl, false); 44 if (ctx == NULL) 45 { 46 printf ("cannot create assembler context: %s\n", asm_errmsg (-1)); 47 return 1; 48 } 49 50 if (asm_newabssym (ctx, "tst8-out.s", 4, 0xfeedbeef, STT_FILE, STB_LOCAL) 51 == NULL) 52 { 53 printf ("cannot create absolute symbol: %s\n", asm_errmsg (-1)); 54 asm_abort (ctx); 55 return 1; 56 } 57 58 /* Create the output file. */ 59 if (asm_end (ctx) != 0) 60 { 61 printf ("cannot create output file: %s\n", asm_errmsg (-1)); 62 asm_abort (ctx); 63 return 1; 64 } 65 66 /* Check the file. */ 67 fd = open (fname, O_RDONLY); 68 if (fd == -1) 69 { 70 printf ("cannot open generated file: %m\n"); 71 result = 1; 72 goto out; 73 } 74 75 elf = elf_begin (fd, ELF_C_READ, NULL); 76 if (elf == NULL) 77 { 78 printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1)); 79 result = 1; 80 goto out_close; 81 } 82 if (elf_kind (elf) != ELF_K_ELF) 83 { 84 puts ("not a valid ELF file"); 85 result = 1; 86 goto out_close2; 87 } 88 89 for (cnt = 1; 1; ++cnt) 90 { 91 Elf_Scn *scn; 92 GElf_Shdr shdr_mem; 93 GElf_Shdr *shdr; 94 95 scn = elf_getscn (elf, cnt); 96 if (scn == NULL) 97 { 98 printf ("cannot get section %Zd: %s\n", cnt, elf_errmsg (-1)); 99 result = 1; 100 continue; 101 } 102 103 shdr = gelf_getshdr (scn, &shdr_mem); 104 if (shdr == NULL) 105 { 106 printf ("cannot get section header for section %Zd: %s\n", 107 cnt, elf_errmsg (-1)); 108 result = 1; 109 continue; 110 } 111 /* We are looking for the symbol table. */ 112 if (shdr->sh_type != SHT_SYMTAB) 113 continue; 114 115 for (cnt = 1; cnt< (shdr->sh_size 116 / gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT)); 117 ++cnt) 118 { 119 GElf_Sym sym_mem; 120 GElf_Sym *sym; 121 122 if (cnt > 1) 123 { 124 puts ("too many symbol"); 125 result = 1; 126 break; 127 } 128 129 sym = gelf_getsym (elf_getdata (scn, NULL), cnt, &sym_mem); 130 if (sym == NULL) 131 { 132 printf ("cannot get symbol %zu: %s\n", cnt, elf_errmsg (-1)); 133 result = 1; 134 } 135 else 136 { 137 if (sym->st_shndx != SHN_ABS) 138 { 139 printf ("expected common symbol, got section %u\n", 140 (unsigned int) sym->st_shndx); 141 result = 1; 142 } 143 144 if (sym->st_value != 0xfeedbeef) 145 { 146 printf ("requested value 0xfeedbeef, is %#" PRIxMAX "\n", 147 (uintmax_t) sym->st_value); 148 result = 1; 149 } 150 151 if (sym->st_size != 4) 152 { 153 printf ("requested size 4, is %" PRIuMAX "\n", 154 (uintmax_t) sym->st_value); 155 result = 1; 156 } 157 158 if (GELF_ST_TYPE (sym->st_info) != STT_FILE) 159 { 160 printf ("requested type FILE, is %u\n", 161 (unsigned int) GELF_ST_TYPE (sym->st_info)); 162 result = 1; 163 } 164 } 165 } 166 167 break; 168 } 169 170 out_close2: 171 elf_end (elf); 172 out_close: 173 close (fd); 174 out: 175 /* We don't need the file anymore. */ 176 unlink (fname); 177 178 ebl_closebackend (ebl); 179 180 return result; 181} 182