Annotation of src/usr.bin/nm/elf.c, Revision 1.8
1.8 ! mickey 1: /* $OpenBSD: elf.c,v 1.7 2004/08/19 19:23:08 mickey Exp $ */
1.1 mickey 2:
3: /*
4: * Copyright (c) 2003 Michael Shalayeff
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: *
16: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19: * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
20: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22: * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25: * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26: * THE POSSIBILITY OF SUCH DAMAGE.
27: */
28:
29: #if ELF_TARG_CLASS == ELFCLASS32
30: #define swap_addr swap32
31: #define swap_off swap32
32: #define swap_sword swap32
33: #define swap_word swap32
34: #define swap_sxword swap32
35: #define swap_xword swap32
36: #define swap_half swap16
37: #define swap_quarter swap16
38: #elif ELF_TARG_CLASS == ELFCLASS64
39: #define swap_addr swap64
40: #define swap_off swap64
41: #ifdef __alpha__
42: #define swap_sword swap64
43: #define swap_word swap64
44: #else
45: #define swap_sword swap32
46: #define swap_word swap32
47: #endif
48: #define swap_sxword swap64
49: #define swap_xword swap64
50: #define swap_half swap64
51: #define swap_quarter swap16
52: #else
53: #error "Unsupported ELF class"
54: #endif
55:
1.3 mickey 56: #define ELF_SBSS ".sbss"
1.6 mickey 57: #define ELF_PLT ".plt"
1.3 mickey 58:
1.1 mickey 59: int
60: elf_fix_header(Elf_Ehdr *eh)
61: {
62: /* nothing to do */
63: if (eh->e_ident[EI_DATA] == ELF_TARG_DATA)
64: return (0);
65:
66: eh->e_type = swap16(eh->e_type);
67: eh->e_machine = swap16(eh->e_machine);
68: eh->e_version = swap32(eh->e_version);
69: eh->e_entry = swap_addr(eh->e_entry);
70: eh->e_phoff = swap_off(eh->e_phoff);
71: eh->e_shoff = swap_off(eh->e_shoff);
72: eh->e_flags = swap32(eh->e_flags);
73: eh->e_ehsize = swap16(eh->e_ehsize);
74: eh->e_phentsize = swap16(eh->e_phentsize);
75: eh->e_phnum = swap16(eh->e_phnum);
76: eh->e_shentsize = swap16(eh->e_shentsize);
77: eh->e_shnum = swap16(eh->e_shnum);
78: eh->e_shstrndx = swap16(eh->e_shstrndx);
79:
80: return (1);
81: }
82:
83: int
84: elf_fix_shdrs(Elf_Ehdr *eh, Elf_Shdr *shdr)
85: {
86: int i;
87:
88: /* nothing to do */
89: if (eh->e_ident[EI_DATA] == ELF_TARG_DATA)
90: return (0);
91:
92: for (i = eh->e_shnum; i--; shdr++) {
93: shdr->sh_name = swap32(shdr->sh_name);
94: shdr->sh_type = swap32(shdr->sh_type);
95: shdr->sh_flags = swap_xword(shdr->sh_flags);
96: shdr->sh_addr = swap_addr(shdr->sh_addr);
97: shdr->sh_offset = swap_off(shdr->sh_offset);
98: shdr->sh_size = swap_xword(shdr->sh_size);
99: shdr->sh_link = swap32(shdr->sh_link);
100: shdr->sh_info = swap32(shdr->sh_info);
101: shdr->sh_addralign = swap_xword(shdr->sh_addralign);
102: shdr->sh_entsize = swap_xword(shdr->sh_entsize);
103: }
104:
105: return (1);
106: }
107:
108: int
109: elf_fix_phdrs(Elf_Ehdr *eh, Elf_Phdr *phdr)
110: {
111: int i;
112:
113: /* nothing to do */
114: if (eh->e_ident[EI_DATA] == ELF_TARG_DATA)
115: return (0);
116:
117: for (i = eh->e_phnum; i--; phdr++) {
118: phdr->p_type = swap32(phdr->p_type);
119: phdr->p_flags = swap32(phdr->p_flags);
120: phdr->p_offset = swap_off(phdr->p_offset);
121: phdr->p_vaddr = swap_addr(phdr->p_vaddr);
122: phdr->p_paddr = swap_addr(phdr->p_paddr);
123: phdr->p_filesz = swap_xword(phdr->p_filesz);
124: phdr->p_memsz = swap_xword(phdr->p_memsz);
125: phdr->p_align = swap_xword(phdr->p_align);
126: }
127:
128: return (1);
129: }
130:
131: int
132: elf_fix_sym(Elf_Ehdr *eh, Elf_Sym *sym)
133: {
134: /* nothing to do */
135: if (eh->e_ident[EI_DATA] == ELF_TARG_DATA)
136: return (0);
137:
138: sym->st_name = swap32(sym->st_name);
139: sym->st_shndx = swap16(sym->st_shndx);
140: sym->st_value = swap_addr(sym->st_value);
141: sym->st_size = swap_xword(sym->st_size);
142:
143: return (1);
144: }
145:
146: /*
147: * Devise nlist's type from Elf_Sym.
148: * XXX this task is done as well in libc and kvm_mkdb.
149: */
150: int
151: elf2nlist(Elf_Sym *sym, Elf_Ehdr *eh, Elf_Shdr *shdr, char *shstr, struct nlist *np)
152: {
153: const char *sn;
154:
155: if (sym->st_shndx < eh->e_shnum)
156: sn = shstr + shdr[sym->st_shndx].sh_name;
157: else
158: sn = "";
1.6 mickey 159: #if 0
160: {
161: extern char *stab;
162: printf("%d:%s %d %s\n", sym->st_shndx, sn,
163: ELF_ST_TYPE(sym->st_info), stab + sym->st_name);
164: }
165: #endif
1.1 mickey 166: switch(ELF_ST_TYPE(sym->st_info)) {
167: case STT_NOTYPE:
168: switch (sym->st_shndx) {
169: case SHN_UNDEF:
170: np->n_type = N_UNDF | N_EXT;
171: break;
172: case SHN_ABS:
173: np->n_type = N_ABS;
174: break;
175: case SHN_COMMON:
176: np->n_type = N_COMM;
177: break;
178: default:
179: if (sym->st_shndx >= eh->e_shnum)
180: np->n_type = N_COMM | N_EXT;
181: else if (!strcmp(sn, ELF_TEXT))
182: np->n_type = N_TEXT;
183: else if (!strcmp(sn, ELF_RODATA)) {
184: np->n_type = N_DATA;
185: np->n_other = 'r';
186: } else if (!strcmp(sn, ELF_DATA))
187: np->n_type = N_DATA;
1.6 mickey 188: else if (!strncmp(sn, ELF_GOT, sizeof(ELF_GOT) - 1))
189: np->n_type = N_DATA;
190: else if (!strncmp(sn, ELF_PLT, sizeof(ELF_PLT) - 1))
191: np->n_type = N_DATA;
1.1 mickey 192: else if (!strcmp(sn, ELF_BSS))
1.5 mickey 193: np->n_type = N_BSS;
1.3 mickey 194: else if (!strcmp(sn, ELF_SBSS))
1.1 mickey 195: np->n_type = N_BSS;
196: else
197: np->n_other = '?';
198: break;
199: }
200: break;
201:
1.8 ! mickey 202: case STT_OBJECT:
! 203: np->n_type = N_DATA;
! 204: switch (sym->st_shndx) {
! 205: case SHN_ABS:
! 206: np->n_type = N_ABS;
! 207: break;
! 208: case SHN_COMMON:
! 209: np->n_type = N_COMM;
! 210: break;
! 211: default:
! 212: if (sym->st_shndx >= eh->e_shnum)
! 213: break;
! 214: else if (!strcmp(sn, ELF_SBSS))
! 215: np->n_type = N_BSS;
! 216: else if (!strcmp(sn, ELF_BSS))
! 217: np->n_type = N_BSS;
! 218: else if (!strcmp(sn, ELF_RODATA))
! 219: np->n_other = 'r';
! 220: }
! 221: break;
! 222:
1.1 mickey 223: case STT_FUNC:
224: np->n_type = N_TEXT;
225: if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
226: np->n_type = N_INDR;
227: np->n_other = 'W';
1.7 mickey 228: } else if (sym->st_shndx == SHN_ABS)
229: np->n_type = N_ABS;
230: else if (sym->st_shndx == SHN_UNDEF)
1.1 mickey 231: np->n_type = N_UNDF | N_EXT;
1.6 mickey 232: else if (strcmp(sn, ELF_INIT) &&
233: strcmp(sn, ELF_TEXT) &&
234: strcmp(sn, ELF_FINI)) /* XXX GNU compat */
1.1 mickey 235: np->n_other = '?';
236: break;
237:
1.8 ! mickey 238: case STT_SECTION:
! 239: switch (sym->st_shndx) {
! 240: case SHN_ABS:
1.1 mickey 241: np->n_type = N_ABS;
1.8 ! mickey 242: break;
! 243: case SHN_COMMON:
1.1 mickey 244: np->n_type = N_COMM;
245: break;
1.8 ! mickey 246: default:
! 247: np->n_other = '?';
! 248: }
1.1 mickey 249: break;
250:
251: case STT_FILE:
252: np->n_type = N_FN | N_EXT;
253: break;
254:
255: /* XXX how about cross-nm then ? */
256: #ifdef STT_PARISC_MILLI
257: case STT_PARISC_MILLI:
258: np->n_type = N_TEXT;
259: break;
260: #endif
261: default:
262: np->n_other = '?';
263: break;
264: }
265: if (np->n_type != N_UNDF && ELF_ST_BIND(sym->st_info) != STB_LOCAL) {
266: np->n_type |= N_EXT;
267: if (np->n_other)
268: np->n_other = toupper(np->n_other);
269: }
270:
271: return (0);
272: }