blob: d207f45fc4c7ab034e16fd17147167e1dc518de2 [file] [log] [blame]
Patrick Georgi7333a112020-05-08 20:48:04 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin54ef3062014-03-05 12:12:09 -06002
3#ifndef ELFPARSING_H
4#define ELFPARSING_H
5
6#include "elf.h"
Aaron Durbinc3e6e142014-03-05 14:33:42 -06007#include "common.h"
Aaron Durbin54ef3062014-03-05 12:12:09 -06008
Aaron Durbind0f61652014-03-05 13:09:55 -06009struct parsed_elf {
10 Elf64_Ehdr ehdr;
11 Elf64_Phdr *phdr;
12 Elf64_Shdr *shdr;
Aaron Durbinccb5ad82014-03-05 13:57:30 -060013 /*
14 * The relocs array contains pointers to arrays of relocation
15 * structures. Each index into the relocs array corresponds to its
16 * corresponding section index. i.e. if a section i is of type SHT_REL
17 * or SHT_RELA then the corresponding index into the relocs array will
18 * contain the associated relocations. Otherwise thee entry will be
19 * NULL.
20 */
21 Elf64_Rela **relocs;
Aaron Durbinc3e6e142014-03-05 14:33:42 -060022 /*
23 * Similarly to the relocs array the strtabs array consists of an
24 * array of pointers where each entry represents a potential struct
25 * buffer pointer. Only setions of type SHT_STRTAB will have a non-NULL
26 * entry.
27 */
28 struct buffer **strtabs;
Aaron Durbinc0780942014-03-05 16:41:27 -060029 /* Parsed symbols. */
30 Elf64_Sym *syms;
Aaron Durbind0f61652014-03-05 13:09:55 -060031};
32
33#define ELF_PARSE_PHDR (1 << 0)
34#define ELF_PARSE_SHDR (1 << 1)
Aaron Durbinccb5ad82014-03-05 13:57:30 -060035#define ELF_PARSE_RELOC (1 << 2)
Aaron Durbinc3e6e142014-03-05 14:33:42 -060036#define ELF_PARSE_STRTAB (1 << 3)
Aaron Durbinc0780942014-03-05 16:41:27 -060037#define ELF_PARSE_SYMTAB (1 << 4)
Aaron Durbind0f61652014-03-05 13:09:55 -060038
39#define ELF_PARSE_ALL (-1)
40
41/*
42 * Parse an ELF file contained within provide struct buffer. The ELF header
43 * is always parsed while the flags value containing the ELF_PARSE_* values
44 * determine if other parts of the ELF file will be parsed as well.
45 * Returns 0 on success, < 0 error.
46 */
47int parse_elf(const struct buffer *pinput, struct parsed_elf *pelf, int flags);
48
49/*
50 * Clean up memory associated with parsed_elf.
51 */
52void parsed_elf_destroy(struct parsed_elf *pelf);
53
54
Aaron Durbin54ef3062014-03-05 12:12:09 -060055int
56elf_headers(const struct buffer *pinput,
Aaron Durbin54ef3062014-03-05 12:12:09 -060057 Elf64_Ehdr *ehdr,
58 Elf64_Phdr **pphdr,
59 Elf64_Shdr **pshdr);
60
Aaron Durbin36be8132014-03-11 11:48:56 -050061/* ELF writing support. */
62struct elf_writer;
63
64/*
Aaron Durbin4f930c92015-10-27 16:21:55 -050065 * Initialize a 64-bit ELF header provided the inputs. While the structure
66 * is a 64-bit header one can specify a 32-bit machine. The 64-bit version
67 * is just used as a common structure. If one wants to specify the entry
68 * point, for example, the caller can set it after filling in the common
69 * bits. The machine, nbits, and endian values should be from the ELF
70 * definitions (e.g. EM_386, ELFCLASS32, and ELFDATA2LSB) found in elf.h
71 * with no endian conversion required.
72 */
73void elf_init_eheader(Elf64_Ehdr *ehdr, int machine, int nbits, int endian);
74
75/*
Elyes HAOUAS3db01982018-08-23 18:08:20 +020076 * Initialize a new ELF writer. Default machine type, endianness, etc is
Aaron Durbin36be8132014-03-11 11:48:56 -050077 * copied from the passed in Elf64_Ehdr. Returns NULL on failure, valid
78 * pointer on success.
79 */
80struct elf_writer *elf_writer_init(const Elf64_Ehdr *ehdr);
81
82/*
83 * Clean up any internal state represented by ew. Aftewards the elf_writer
84 * is invalid.
Furquan Shaikhb927bec2016-08-05 12:04:55 -070085 * It is safe to call elf_writer_destroy with ew as NULL. It returns without
86 * performing any action.
Aaron Durbin36be8132014-03-11 11:48:56 -050087 */
88void elf_writer_destroy(struct elf_writer *ew);
89
90/*
91 * Add a section to the ELF file. Section type, flags, and memsize are
92 * maintained from the passed in Elf64_Shdr. The buffer represents the
93 * content of the section while the name is the name of section itself.
94 * Returns < 0 on error, 0 on success.
95 */
96int elf_writer_add_section(struct elf_writer *ew, const Elf64_Shdr *shdr,
97 struct buffer *contents, const char *name);
98
Aaron Durbincedcb882015-10-28 11:26:40 -050099/* Add an absolute symbol to the ELF file returning < 0 on error, index of
100 * symbol otherwise. */
101int elf_writer_add_symbol(struct elf_writer *ew, const char *name,
102 const char *section_name,
103 Elf64_Addr value, Elf64_Word size,
104 int binding, int type);
105
106/* Add an absolute relocation referencing the provided symbol name. Returns < 0
107 * on error, 0 on success. */
108int elf_writer_add_rel(struct elf_writer *ew, const char *sym, Elf64_Addr addr);
109
Aaron Durbin36be8132014-03-11 11:48:56 -0500110/*
111 * Serialize the ELF file to the output buffer. Return < 0 on error,
112 * 0 on success.
113 */
114int elf_writer_serialize(struct elf_writer *ew, struct buffer *out);
115
Aaron Durbinfacf1492017-12-18 14:50:22 -0700116/*
117 * Calculate the loadable program's file size footprint. Returns < 0 on error,
118 * 0 on success.
119 */
120int elf_program_file_size(const struct buffer *input, size_t *file_size);
121
Aaron Durbin54ef3062014-03-05 12:12:09 -0600122#endif /* ELFPARSING_H */