https://github.com/0intro/libelf
Libelf is a simple library to read ELF files.
https://github.com/0intro/libelf
Last synced: 4 months ago
JSON representation
Libelf is a simple library to read ELF files.
- Host: GitHub
- URL: https://github.com/0intro/libelf
- Owner: 0intro
- License: mit
- Created: 2016-11-21T19:54:50.000Z (about 9 years ago)
- Default Branch: main
- Last Pushed: 2022-04-04T20:05:17.000Z (almost 4 years ago)
- Last Synced: 2025-04-07T20:56:21.487Z (9 months ago)
- Language: C
- Homepage:
- Size: 25.4 KB
- Stars: 49
- Watchers: 6
- Forks: 12
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-c - libelf - Simple library for parsing ELF files. [MIT](https://spdx.org/licenses/MIT.html) (Structured File Processing / Others)
- awesome-c-zh - libelf - 用于解析ELF文件的简单库。[](https://spdx.org/licenses/MIT.html) (结构化文件处理 / 其他)
- awesome-c - libelf - Simple library for parsing ELF files. [MIT](https://spdx.org/licenses/MIT.html) (Structured File Processing / Others)
README
[](https://github.com/0intro/libelf/actions/workflows/c.yml)
[](https://scan.coverity.com/projects/0intro-libelf)
libelf
======
Libelf is a simple library which provides functions to read ELF files.
Headers
-------
```
#include
#include
```
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 */
/* ELF Header */
uint16_t type;
uint16_t machine;
uint32_t version;
uint64_t entry;
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
---------
```
/* Read */
int readelf(FILE *f, Fhdr *fp);
uint8_t* readelfsection(FILE *f, char *name, uint64_t *size, Fhdr *fp);
void freeelf(Fhdr *fp);
/* Print */
void printelfhdr(Fhdr *fp);
/* String */
char* elfclass(uint8_t class);
char* elfdata(uint8_t data);
char* elfosabi(uint8_t osabi);
char* elftype(uint16_t type);
char* elfmachine(uint16_t machine);
char* elfversion(uint8_t version);
```
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);
```