.
[glibc/history.git] / elf / libelf.h
blobb6575b9824dccf671458bfef18cd17ae6cebc5a4
1 /* Interface for manipulating ELF object files; functions found in -lelf.
2 Copyright (C) 1995 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
20 #ifndef _LIBELF_H
21 #define _LIBELF_H 1
23 #include <sys/cdefs.h>
24 #include <sys/types.h>
25 #include <elf.h>
27 __BEGIN_DECLS
29 /* Commands to operate on an Elf descriptor.
30 The meanings are slightly different for the different functions. */
31 typedef enum
33 ELF_C_NULL = 0,
34 ELF_C_READ, /* Read from the file. */
35 ELF_C_WRITE, /* Write the file, ignoring old contents. */
36 ELF_C_CLR, /* Clear specified flag bits. */
37 ELF_C_SET, /* Set specified flag bits. */
38 ELF_C_FDDONE, /* Close the fd; no further io will happen. */
39 ELF_C_FDREAD, /* Read the whole file, then close it. */
40 ELF_C_RDWR, /* Read from and modify the file. */
41 ELF_C_NUM /* Number of valid command values. */
42 } Elf_Cmd;
45 /* Flag bits for `elf_flag*'. */
46 #define ELF_F_DIRTY 0x1 /* Object is */
47 #define ELF_F_LAYOUT 0x4
50 /* File types. */
51 typedef enum
53 ELF_K_NONE = 0,
54 ELF_K_AR,
55 ELF_K_COFF, /* The GNU -lelf does not support COFF. */
56 ELF_K_ELF,
57 ELF_K_NUM /* Number of valid file kinds. */
58 } Elf_Kind;
61 /* Translation types. */
62 typedef enum
64 ELF_T_BYTE = 0,
65 ELF_T_ADDR,
66 ELF_T_DYN,
67 ELF_T_EHDR,
68 ELF_T_HALF,
69 ELF_T_OFF,
70 ELF_T_PHDR,
71 ELF_T_RELA,
72 ELF_T_REL,
73 ELF_T_SHDR,
74 ELF_T_SWORD,
75 ELF_T_SYM,
76 ELF_T_WORD,
77 ELF_T_NUM
78 } Elf_Type;
81 /* These types are opaque to user code; only pointers to them are used. */
82 typedef struct Elf Elf;
83 typedef struct Elf_Scn Elf_Scn;
86 /* Archive member header. */
87 typedef struct
89 char *ar_name;
90 time_t ar_date;
91 uid_t ar_uid;
92 gid_t ar_gid;
93 mode_t ar_mode;
94 off_t ar_size;
95 char *ar_rawname;
96 } Elf_Arhdr;
98 /* Archive symbol table. */
99 typedef struct
101 char *as_name;
102 size_t as_off;
103 unsigned long int as_hash;
104 } Elf_Arsym;
107 /* Data descriptor. */
108 typedef struct
110 void *d_buf;
111 Elf_Type d_type;
112 size_t d_size;
113 off_t d_off; /* Offset into section. */
114 size_t d_align; /* Alignment in section. */
115 unsigned int d_version; /* ELF version. */
116 } Elf_Data;
120 /* Open an Elf descriptor on file descriptor FD.
121 REF is the Elf descriptor for the containing archive (to open a member);
122 or the descriptor previously returned for FD (to add a user reference);
123 or NULL. */
124 extern Elf *elf_begin __P ((int __fd, Elf_Cmd __cmd, Elf *__ref));
126 /* Finish using ELF (remove a user reference); if this is the last user
127 reference, its data will be freed. */
128 extern int elf_end __P ((Elf *__elf));
130 /* Control the library's access to the file descriptor for ELF.
131 CMD is either ELF_C_FDDONE or ELF_C_FDREAD. */
132 extern int elf_cntl __P ((Elf *__elf, Elf_Cmd __cmd));
135 /* Return a string describing an ELF error number. */
136 extern __const char *elf_errmsg __P ((int __errno));
138 /* Return the ELF error number for the last failed operation. */
139 extern int elf_errno __P ((void));
141 /* Set the byte value used to fill sections for alignment. */
142 extern void elf_fill __P ((int __fillchar));
144 /* The following functions `elf_flag*' all operate the same way:
145 CMD is either ELF_C_SET or ELF_C_CLR; FLAGS are `ELF_F_*' above,
146 which are set or cleared for the object the call relates to. */
148 /* Modify flags affecting the file as a whole (?). */
149 extern unsigned int elf_flagelf __P ((Elf *__elf, Elf_Cmd __cmd,
150 unsigned int __flags));
151 /* Modify flags affecting DATA. */
152 extern unsigned int elf_flagdata __P ((Elf_Data *__data, Elf_Cmd __cmd,
153 unsigned int __flags));
154 /* Modify flags affecting the ELF header. */
155 extern unsigned int elf_flagehdr __P ((Elf *__elf, Elf_Cmd __cmd,
156 unsigned int __flags));
157 /* Modify flags affecting the ELF program header. */
158 extern unsigned int elf_flagphdr __P ((Elf *__elf, Elf_Cmd __cmd,
159 unsigned int __flags));
160 /* Modify flags affecting the given section's data. */
161 extern unsigned int elf_flagscn __P ((Elf_Scn *__scn, Elf_Cmd __cmd,
162 unsigned int __flags));
163 /* Modify flags affecting the given section's header. */
164 extern unsigned int elf_flagshdr __P ((Elf_Scn *__scn, Elf_Cmd __cmd,
165 unsigned int __flags));
168 extern size_t elf32_fsize __P ((Elf_Type __type, size_t __count,
169 unsigned int __ver));
171 /* Return the archive header for ELF, which must describe an archive. */
172 extern Elf_Arhdr *elf_getarhdr __P ((Elf *__elf));
174 /* Return the archive symbol table for ELF, and store
175 in *NELTSP the number of elements in the table. */
176 extern Elf_Arsym *elf_getarsym __P ((Elf *__elf, size_t *__neltsp));
178 /* Return the file offset for the beginning of ELF.
179 If ELF describes an archive member, this points to the member header. */
180 extern off_t elf_getbase __P ((Elf *__elf));
182 /* Extract the data from a section. */
183 extern Elf_Data *elf_getdata __P ((Elf_Scn *__scn, Elf_Data *__data));
185 /* Extract the ELF header from the file. */
186 extern Elf32_Ehdr *elf32_getehdr __P ((Elf *__elf));
188 /* Extract the initial ELF identification bytes from the file.
189 If PTR is nonnull, the number of identification bytes is stored there. */
190 extern char *elf_getident __P((Elf *__elf, size_t *__ptr));
192 /* Extract the ELF program header from the file. */
193 extern Elf32_Phdr *elf32_getphdr __P ((Elf *__elf));
195 /* Extract the indicated section from the file. */
196 extern Elf_Scn *elf_getscn __P ((Elf *__elf, size_t __index));
198 /* Extract the section header from the section. */
199 extern Elf32_Shdr *elf32_getshdr __P ((Elf_Scn *__scn));
201 /* Return the index of the section following SCN. */
202 extern size_t elf_ndxscn __P ((Elf_Scn *__scn));
205 /* Standard ELF symbol name hash function. */
206 extern unsigned long int elf_hash __P ((__const char *__name));
208 #if defined (__OPTIMIZE__) || defined (_EXTERN_INLINE)
209 #ifndef _EXTERN_INLINE
210 #define _EXTERN_INLINE extern __inline
211 #endif
212 _EXTERN_INLINE unsigned long int
213 elf_hash (__const char *__name)
215 /* This is the hashing function specified by the ELF ABI. */
216 unsigned long int __hash = 0;
217 while (*__name != '\0')
219 unsigned long int __hi;
220 __hash = (__hash << 4) + *__name++;
221 __hi = __hash & 0xf0000000;
222 if (__hi != 0)
224 __hash ^= __hi >> 24;
225 /* The ELF ABI says `hash &= ~hi', but this is equivalent
226 in this case and on some machines one insn instead of two. */
227 __hash ^= __hi;
230 return __hash;
232 #endif
234 /* Return the kind of file ELF describes. */
235 extern Elf_Kind elf_kind __P ((Elf *__elf));
237 extern Elf_Data *elf_newdata __P ((Elf_Scn *__scn));
239 /* Create the ELF header for ELF. */
240 extern Elf32_Ehdr *elf32_newehdr __P ((Elf *__elf));
242 /* Create the program header for ELF, with COUNT segments. */
243 extern Elf32_Phdr *elf32_newphdr __P ((Elf *__elf, size_t __count));
245 /* Create a new section in ELF. */
246 extern Elf_Scn *elf_newscn __P ((Elf *__elf));
248 /* Return the section following SCN. */
249 extern Elf_Scn *elf_nextscn __P ((Elf *__elf, Elf_Scn *__scn));
251 /* Set up ELF to read the next archive member. */
252 extern Elf_Cmd elf_next __P ((Elf *__elf));
254 /* Set up ELF (which must describe an archive) to read the
255 archive member that starts at file position OFFSET. */
256 extern size_t elf_rand __P ((Elf *__elf, size_t __offset));
258 extern Elf_Data *elf_rawdata __P ((Elf_Scn *__scn, Elf_Data *__data));
260 /* Read the entire file into memory; store its size in *PTR. */
261 extern char *elf_rawfile __P ((Elf *__elf, size_t *__ptr));
263 /* Return a pointer to the string at OFFSET bytes into the string table.
264 SECTION is the index of the SHT_STRTAB section in ELF. */
265 extern char *elf_strptr __P ((Elf *__elf, size_t __section, size_t __offset));
267 /* If CMD is ELF_C_NULL, update ELF's data structures based on any
268 user modifications, and set the ELF_F_DIRTY flag if anything changed.
269 If CMD is ELF_C_WRITE, do that and then write the changes to the file. */
270 extern off_t elf_update __P ((Elf *__elf, Elf_Cmd __cmd));
272 /* Handle ELF version VER. Return the old version handled,
273 or EV_NONE if VER is unrecognized. */
274 extern unsigned int elf_version __P ((unsigned int __ver));
276 extern Elf_Data *elf32_xlatetof __P ((Elf_Data *__dst, const Elf_Data *__src,
277 unsigned int __encode));
278 extern Elf_Data *elf32_xlatetom __P ((Elf_Data *__dst, const Elf_Data *__src,
279 unsigned int __encode));
281 __END_DECLS
283 #endif /* _LIBELF_H */