libelf/README.md

104 lines
1.9 KiB
Markdown
Raw Permalink Normal View History

2022-04-04 23:03:56 +03:00
[![Build Status](https://github.com/0intro/libelf/workflows/C/badge.svg)](https://github.com/0intro/libelf/actions/workflows/c.yml)
2016-11-22 00:11:53 +03:00
[![Coverity Scan Build Status](https://scan.coverity.com/projects/0intro-libelf/badge.svg)](https://scan.coverity.com/projects/0intro-libelf)
libelf
======
Libelf is a simple library which provides functions to read ELF files.
Headers
-------
```
#include <stdint.h>
#include <elf.h>
```
Structures
----------
```
typedef struct Fhdr Fhdr;
/*
* Portable ELF file header
*/
struct Fhdr {
/* Private */
...
/* ELF Identification */
uint8_t class; /* File class */
uint8_t data; /* Data encoding */
uint8_t elfversion; /* File version */
uint8_t osabi; /* Operating system/ABI identification */
uint8_t abiversion; /* ABI version */
2016-11-22 00:11:53 +03:00
/* ELF Header */
uint16_t type;
uint16_t machine;
uint32_t version;
uint64_t entry;
2016-11-22 00:11:53 +03:00
uint64_t phoff;
uint64_t shoff;
uint16_t ehsize; /* ELF Header size */
uint16_t phentsize; /* Section Header size */
uint16_t phnum;
uint16_t shentsize; /* Program Header size */
uint16_t shnum;
uint16_t shstrndx;
/* Section Header */
uint32_t name;
uint64_t offset;
uint64_t size;
/* String Table */
uint32_t strndxsize; /* String Table Size */
uint8_t *strndx; /* Copy of String Table */
};
```
Functions
---------
```
2017-07-28 00:38:50 +03:00
/* Read */
2016-11-22 00:11:53 +03:00
int readelf(FILE *f, Fhdr *fp);
uint8_t* readelfsection(FILE *f, char *name, uint64_t *size, Fhdr *fp);
void freeelf(Fhdr *fp);
2017-07-28 00:38:50 +03:00
/* Print */
2017-07-28 00:38:26 +03:00
void printelfhdr(Fhdr *fp);
2017-07-28 00:38:50 +03:00
/* String */
2017-07-28 00:36:16 +03:00
char* elfclass(uint8_t class);
2017-07-28 00:36:34 +03:00
char* elfdata(uint8_t data);
2017-07-28 00:36:55 +03:00
char* elfosabi(uint8_t osabi);
2017-07-28 00:32:52 +03:00
char* elftype(uint16_t type);
2017-07-28 00:34:50 +03:00
char* elfmachine(uint16_t machine);
2017-07-28 00:35:57 +03:00
char* elfversion(uint8_t version);
2016-11-22 00:11:53 +03:00
```
Example
-------
```
Fhdr fhdr;
FILE *f;
uint8_t *buf;
uint64_t len;
f = fopen("/bin/ls", "rb");
if (f == NULL)
return -1;
buf = readelfsection(f, ".text", &len, &fhdr);
if (buf == NULL)
return -1;
// ...
freeelf(&fhdr);
```