new-bu"> ]> New Binutils User's and Reference Manual <filename>libelf</filename> <acronym>ABI</acronym> The ABI of the libelf implemented in the &package; package is following that of Sun's implementation which in turn in derived from the original SysVr4 implementation. There are some extensions over Sun's versions, though, which makes it impossible to replace this implementation with Sun's. Elf_Data Descriptor for Data Buffer #include <libelf.h> Description The Elf_Data structure is as a descriptor for a data buffer associated with a section. Every data buffer is associated with a specific section (see Elf_Scn). A data buffer is created when reading a file. In this case only a single buffer is present in the section. The user can add as many sections as wanted to a section and they can be retrieved using the elf_getdata and elf_rawdata functions. The Elf_Data structure contains the following members: void *d_buf Elf_Type d_type size_t d_size off_t d_off size_t d_align unsigned int d_version All of these members can be modified directly by the user. They can be used to resize a section, to change its content or type, and many more tasks. This is also true for the data read from a file. The meaning of each member is as follows: d_buf The d_buf member is the pointer to the buffer with the actual data. When the ELF file was read from a file the first and only data buffer of a section is allocated by the libelf library. The user should not try to resize or free this buffer. When the user adds a new data buffer to a section the associated memory block is normally allocated by the user. It is important that the buffer must have a lifetime at least until the ELF file is closed entirely (important when the buffer is allocated on the stack). If the buffer is not allocated on the stack it is the user's responsibility to free the buffer after it is not used anymore. The d_buf member can contain a null pointer if the data buffer is empty. d_type The d_type determines how the data of the buffer is interpreted. This type is determined from the section type and must be the same for all data buffers for a section. See Elf_Type for more information. The function uses this information to convert the data of the buffer between the external form and the form represented to the user and back if necessary. d_version The d_version contains the ELF version of the file. d_size The d_size contains the size of the buffer in bytes. d_off The d_off is the offset into the section in bytes. d_align The d_align is the address alignment of the section in bytes. elf_getdata Get washed data of section #include <libelf.h> Elf_Data *elf_getdata Elf_Scn *scn Elf_Data *data Description The elf_getdata function allows the user to retrieve the data buffers of the section scn. There can be more than one buffer if the user explicitly added them. When a file is read the libelf library creates exactly one data buffer. The first buffer in the list can be obtained by passing a null pointer in the parameter data. To get the next data buffer the previously returned value must be passed in the data parameter. If there are no more buffer left in the list a null pointer is returned. If the data parameter is not a null pointer it must be a descriptor for a buffer associated with the section scn. If this is not the case a null pointer is returned. To facilitate error handling elf_getdata also returns a null pointer if the scn parameter is a null pointer. elf_update update an ELF descriptor #include <libelf.h> off_t elf_update Elf *elf Elf_Cmd cmd Description The user is responsible for filling in the following fields in the named data structures: Fields not set by <function>elf_update</function> Data Structure Member Exception Elfxx_Ehdr e_ident[EI_DATA] see below e_type e_machine e_version see below e_entry e_phoff if ELF_F_LAYOUT is used e_shoff if ELF_F_LAYOUT is used e_flags e_shstrndx Elfxx_Phdr p_type p_offset p_vaddr p_paddr p_filesz p_memsz p_flags p_align Elfxx_Shdr sh_name sh_type sh_flags sh_addr sh_offset if ELF_F_LAYOUT is used sh_size if ELF_F_LAYOUT is used sh_link sh_info sh_addralign if ELF_F_LAYOUT is used sh_entsize Elf_Data d_buf d_type d_size d_off if ELF_F_LAYOUT is used d_align d_version
Two fields of the ELF header are handled in a special way: e_version The user can set this field to the vvalue for the version to be used. It is an error if the library cannot handle this version. If the field contains the value EV_NONE the library will fill in its own internal version. e_ident[EI_DATA] The user should fill in the byte ordering for the file. If the value of the field is ELFDATANONE the library replaces it with the native byte ordering for the machine.
<filename>libelf</filename> Internals Since the binary format handling tools need constant attention since there are always new machines and varients therefore coming out it is important to have the implementation well documented. Only this way extensions can be made in the right places and the mistakes of the past avoided.