xref: /aosp_15_r20/external/coreboot/util/cbfstool/elfparsing.h (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #ifndef ELFPARSING_H
4 #define ELFPARSING_H
5 
6 #include "elf.h"
7 #include "common.h"
8 
9 struct parsed_elf {
10 	Elf64_Ehdr ehdr;
11 	Elf64_Phdr *phdr;
12 	Elf64_Shdr *shdr;
13 	/*
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;
22 	/*
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;
29 	/* Parsed symbols. */
30 	Elf64_Sym *syms;
31 };
32 
33 #define ELF_PARSE_PHDR		(1 << 0)
34 #define ELF_PARSE_SHDR		(1 << 1)
35 #define ELF_PARSE_RELOC		(1 << 2)
36 #define ELF_PARSE_STRTAB	(1 << 3)
37 #define ELF_PARSE_SYMTAB	(1 << 4)
38 
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  */
47 int parse_elf(const struct buffer *pinput, struct parsed_elf *pelf, int flags);
48 
49 /*
50  * Clean up memory associated with parsed_elf.
51  */
52 void parsed_elf_destroy(struct parsed_elf *pelf);
53 
54 
55 int
56 elf_headers(const struct buffer *pinput,
57 	    Elf64_Ehdr *ehdr,
58 	    Elf64_Phdr **pphdr,
59 	    Elf64_Shdr **pshdr);
60 
61 /* ELF writing support. */
62 struct elf_writer;
63 
64 /*
65  * 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  */
73 void elf_init_eheader(Elf64_Ehdr *ehdr, int machine, int nbits, int endian);
74 
75 /*
76  * Initialize a new ELF writer. Default machine type, endianness, etc is
77  * copied from the passed in Elf64_Ehdr. Returns NULL on failure, valid
78  * pointer on success.
79  */
80 struct 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.
85  * It is safe to call elf_writer_destroy with ew as NULL. It returns without
86  * performing any action.
87  */
88 void 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  */
96 int elf_writer_add_section(struct elf_writer *ew, const Elf64_Shdr *shdr,
97                            struct buffer *contents, const char *name);
98 
99 /* Add an absolute symbol to the ELF file returning < 0 on error, index of
100  * symbol otherwise. */
101 int 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. */
108 int elf_writer_add_rel(struct elf_writer *ew, const char *sym, Elf64_Addr addr);
109 
110 /*
111  * Serialize the ELF file to the output buffer. Return < 0 on error,
112  * 0 on success.
113  */
114 int elf_writer_serialize(struct elf_writer *ew, struct buffer *out);
115 
116 /*
117  * Calculate the loadable program's file size footprint and alignment requirements.
118  * Returns < 0 on error, 0 on success.
119  */
120 int elf_program_file_size_align(const struct buffer *input, size_t *file_size, size_t *align);
121 
122 #endif /* ELFPARSING_H */
123