ELF Library Functions gelf(3ELF)
NAME
gelf, gelfchecksum, gelffsize, gelfgetcap, gelfgetclass,
gelfgetdyn, gelfgetehdr, gelfgetmove, gelfgetphdr,
gelfgetrel, gelfgetrela, gelfgetshdr, gelfgetsym,
gelfgetsyminfo, gelfgetsymshndx, gelfnewehdr,
gelfnewphdr, gelfupdatecap, gelfupdatedyn,
gelfupdateehdr, gelfupdategetmove, gelfupdatemove,
gelfupdatephdr, gelfupdaterel, gelfupdaterela,
gelfupdateshdr, gelfupdatesym, gelfupdatesymshndx,
gelfupdatesyminfo, gelfxlatetof, gelfxlatetom - generic
class-independent ELF interface
SYNOPSIS
cc [ flag... ] file... -lelf [ library... ]
#include
long gelfchecksum(Elf *elf);
sizet gelffsize(Elf *elf, ElfType type, sizet cnt, unsigned ver);
int gelfgetcap(ElfData *src, int ndx, GElfCap *dst);
int gelfgetclass(Elf *elf);
GElfDyn *gelfgetdyn(ElfData *src, int ndx, GElfDyn *dst);
GElfEhdr *gelfgetehdr(Elf *elf, GElfEhdr *dst);
GElfMove *gelfgetmove(ElfData *src, int ndx, GElfMove *dst);
GElfPhdr *gelfgetphdr(Elf *elf, int ndx, GElfPhdr *dst);
GElfRel *gelfgetrel(ElfData *src, int ndx, GElfRel *dst);
GElfRela *gelfgetrela(ElfData *src, int ndx, GElfRela *dst);
GElfShdr *gelfgetshdr(ElfScn *scn, GElfShdr *dst);
GElfSym *gelfgetsym(ElfData *src, int ndx, GElfSym *dst);
SunOS 5.11 Last change: 8 June 2004 1
ELF Library Functions gelf(3ELF)
GElfSyminfo *gelfgetsyminfo(ElfData *src, int ndx, GElfSyminfo *dst);
GElfSym *gelfgetsymshndx(ElfData *symsrc, ElfData *shndxsrc,
int ndx, GElfSym *symdst, Elf32Word *shndxdst);
unsigned long gelfnewehdr(Elf *elf, int class);
unsigned long gelfnewphdr(Elf *elf, sizet phnum);
int gelfupdatecap(ElfData *dst, int ndx, GElfCap *src);
int gelfupdatedyn(ElfData *dst, int ndx, GElfDyn *src);
int gelfupdateehdr(Elf *elf, GElfEhdr *src);
int gelfupdatemove(ElfData *dst, int ndx, GElfMove *src);
int gelfupdatephdr(Elf *elf, int ndx, GElfPhdr *src);
int gelfupdaterel(ElfData *dst, int ndx, GElfRel *src);
int gelfupdaterela(ElfData *dst, int ndx, GElfRela *src);
int gelfupdateshdr(ElfScn *dst, GElfShdr *src);
int gelfupdatesym(ElfData *dst, int ndx, GElfSym *src);
int gelfupdatesyminfo(ElfData *dst, int ndx, GElfSyminfo *src);
int gelfupdatesymshndx(ElfData *symdst, ElfData *shndxdst, int ndx,
GElfSym *symsrc, Elf32Word shndxsrc);
ElfData *gelfxlatetof(Elf *elf, ElfData *dst, const ElfData *src,
unsigned encode);
SunOS 5.11 Last change: 8 June 2004 2
ELF Library Functions gelf(3ELF)
ElfData *gelfxlatetom(Elf *elf, ElfData *dst, const ElfData *src,
unsigned encode);
DESCRIPTION
GElf is a generic, ELF class-independent API for manipulat-
ing ELF object files. GElf provides a single, common inter-
face for handling 32-bit and 64-bit ELF format object files.
GElf is a translation layer between the application and the
class-dependent parts of the ELF library. Thus, the applica-
tion can use GElf, which in turn, will call the correspond-
ing elf32 or elf64 functions on behalf of the application.
The data structures returned are all large enough to hold
32-bit and 64-bit data.
GElf provides a simple, class-independent layer of indirec-
tion over the class-dependent ELF32 and ELF64 API's. GElf is
stateless, and may be used along side the ELF32 and ELF64
API's.
GElf always returns a copy of the underlying ELF32 or ELF64
structure, and therefore the programming practice of using
the address of an ELF header as the base offset for the
ELF's mapping into memory should be avoided. Also, data
accessed by type-casting the ElfData buffer to a class-
dependent type and treating it like an array, for example, a
symbol table, will not work under GElf, and the gelfget
functions must be used instead. See the EXAMPLE section.
Programs that create or modify ELF files using libelf(3LIB)
need to perform an extra step when using GElf. Modifications
to GElf values must be explicitly flushed to the underlying
ELF32 or ELF64 structures by way of the gelfupdate inter-
faces. Use of elfupdate or elfflagelf and the like remains
the same.
The sizes of versioning structures remain the same between
ELF32 and ELF64. The GElf API only defines types for ver-
sioning, rather than a functional API. The processing of
versioning information will stay the same in the GElf
environment as it was in the class-dependent ELF environ-
ment.
List of Functions
gelfchecksum() An analog to elf32checksum(3ELF)
and elf64checksum(3ELF).
SunOS 5.11 Last change: 8 June 2004 3
ELF Library Functions gelf(3ELF)
gelffsize() An analog to elf32fsize(3ELF) and
elf64fsize(3ELF).
gelfgetcap() Retrieves the Elf32Cap or
Elf64Cap information from the
capability table at the given
index. dst points to the location
where the GElfCap capability
entry is stored.
gelfgetclass() Returns one of the constants
ELFCLAS32, ELFCLAS64 or
ELFCLASNONE.
gelfgetdyn() Retrieves the Elf32Dyn or
Elf64Dyn information from the
dynamic table at the given index.
dst points to the location where
the GElfDyn dynamic entry is
stored.
gelfgetehdr() An analog to elf32getehdr(3ELF)
and elf64getehdr(3ELF). dst
points to the location where the
GElfEhdr header is stored.
gelfgetmove() Retrieves the Elf32Move or
Elf64Move information from the
move table at the given index. dst
points to the location where the
GElfMove move entry is stored.
gelfgetphdr() An analog toelf32getphdr(3ELF)
and elf64getphdr(3ELF). dst
points to the location where the
GElfPhdr program header is
stored.
gelfgetrel() Retrieves the Elf32Rel or
Elf64Rel information from the
relocation table at the given
index. dst points to the location
where the GElfRel relocation
entry is stored.
SunOS 5.11 Last change: 8 June 2004 4
ELF Library Functions gelf(3ELF)
gelfgetrela() Retrieves the Elf32Rela or
Elf64Rela information from the
relocation table at the given
index. dst points to the location
where the GElfRela relocation
entry is stored.
gelfgetshdr() An analog to elf32getshdr(3ELF)
and elf64getshdr(3ELF). dst
points to the location where the
GElfShdr section header is
stored.
gelfgetsym() Retrieves the Elf32Sym or
Elf64Sym information from the
symbol table at the given index.
dst points to the location where
the GElfSym symbol entry is
stored.
gelfgetsyminfo() Retrieves the Elf32Syminfo or
Elf64Syminfo information from the
relocation table at the given
index. dst points to the location
where the GElfSyminfo symbol
information entry is stored.
gelfgetsymshndx() Provides an extension to
gelfgetsym() that retrieves the
Elf32Sym or Elf64Sym informa-
tion, and the section index from
the symbol table at the given
index ndx.
The symbols section index is typi-
cally recorded in the stshndx
field of the symbols structure.
However, a file that requires ELF
Extended Sections may record an
stshndx of SHNXINDEX indicating
that the section index must be
obtained from an associated
SHTSYMTABSHNDX section entry. If
xshndx and shndxdata are non-null,
the value recorded at index ndx of
the SHTSYMTABSHNDX table pointed
to by shndxdata is returned in
xshndx. See USAGE.
SunOS 5.11 Last change: 8 June 2004 5
ELF Library Functions gelf(3ELF)
gelfnewehdr() An analog to elf32newehdr(3ELF)
and elf64newehdr(3ELF).
gelfnewphdr() An analog to elf32newphdr(3ELF)
and elf64newphdr(3ELF).
gelfupdatecap() Copies the GElfCap information
back into the underlying Elf32Cap
or Elf64Cap structure at the
given index.
gelfupdatedyn() Copies the GElfDyn information
back into the underlying Elf32Dyn
or Elf64Dyn structure at the
given index.
gelfupdateehdr() Copies the contents of the
GElfEhdr ELF header to the under-
lying Elf32Ehdr or Elf64Ehdr
structure.
gelfupdatemove() Copies the GElfMove information
back into the underlying
Elf32Move or Elf64Move structure
at the given index.
gelfupdatephdr() Copies of the contents of
GElfPhdr program header to under-
lying the Elf32Phdr or Elf64Phdr
structure.
gelfupdaterel() Copies the GElfRel information
back into the underlying Elf32Rel
or Elf64Rel structure at the
given index.
gelfupdaterela() Copies the GElfRela information
back into the underlying
Elf32Rela or Elf64Rela structure
at the given index.
gelfupdateshdr() Copies of the contents of
GElfShdr section header to
SunOS 5.11 Last change: 8 June 2004 6
ELF Library Functions gelf(3ELF)
underlying the Elf32Shdr or
Elf64Shdr structure.
gelfupdatesym() Copies the GElfSym information
back into the underlying Elf32Sym
or Elf64Sym structure at the
given index.
gelfupdatesyminfo() Copies the GElfSyminfo informa-
tion back into the underlying
Elf32Syminfo or Elf64Syminfo
structure at the given index.
gelfupdatesymshndx() Provides an extension to
gelfupdatesym() that copies the
GElfSym information back into the
Elf32Sym or Elf64Sym structure
at the given index ndx, and copies
the extended xshndx section index
into the Elf32Word at the given
index ndx in the buffer described
by shndxdata. See USAGE.
gelfxlatetof() An analog to elf32xlatetof(3ELF)
and elf64xlatetof(3ELF)
gelfxlatetom() An analog to elf32xlatetom(3ELF)
and elf64xlatetom(3ELF)
RETURN VALUES
Upon failure, all GElf functions return 0 and set elferrno.
See elferrno(3ELF)
EXAMPLES
Example 1 Printing the ELF Symbol Table
#include
#include
#include
#include
#include
#include
void
main(int argc, char **argv)
{
SunOS 5.11 Last change: 8 June 2004 7
ELF Library Functions gelf(3ELF)
Elf *elf;
ElfScn *scn = NUL;
GElfShdr shdr;
ElfData *data;
int fd, ii, count;
elfversion(EVCURENT);
fd = open(argv[1], ORDONLY);
elf = elfbegin(fd, ELFCREAD, NUL);
while ((scn = elfnextscn(elf, scn)) != NUL) {
gelfgetshdr(scn, &shdr);
if (shdr.shtype == SHTSYMTAB) {
/* found a symbol table, go print it. */
break;
}
}
data = elfgetdata(scn, NUL);
count = shdr.shsize / shdr.shentsize;
/* print the symbol names */
for (ii = 0; ii < count; ]ii) {
GElfSym sym;
gelfgetsym(data, ii, &sym);
printf("%s\n", elfstrptr(elf, shdr.shlink, sym.stname));
}
elfend(elf);
close(fd);
}
USAGE
ELF Extended Sections are employed to allow an ELF file to
contain more than 0xff00 (SHNLORESERVE) section. See the
Linker and Libraries Guide for more information.
FILES
/lib/libelf.so.1 shared object
/lib/64/libelf.so.1 64-bit shared object
ATRIBUTES
See attributes(5) for descriptions of the following attri-
butes:
SunOS 5.11 Last change: 8 June 2004 8
ELF Library Functions gelf(3ELF)
ATRIBUTE TYPE ATRIBUTE VALUE
Interface Stability Stable
MT Level MT-Safe
SEE ALSO
elf(3ELF), elf32checksum(3ELF), elf32fsize(3ELF),
elf32getehdr(3ELF), elf32newehdr(3ELF),
elf32getphdr(3ELF), elf32newphdr(3ELF),
elf32getshdr(3ELF), elf32xlatetof(3ELF),
elf32xlatetom(3ELF), elferrno(3ELF), libelf(3LIB), attri-
butes(5)
Linker and Libraries Guide
SunOS 5.11 Last change: 8 June 2004 9
|