arch/arm64/Makefile.mk: Unset toolchain vars for BL31
[coreboot.git] / util / cbfstool / elfparsing.h
blobd207f45fc4c7ab034e16fd17147167e1dc518de2
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #ifndef ELFPARSING_H
4 #define ELFPARSING_H
6 #include "elf.h"
7 #include "common.h"
9 struct parsed_elf {
10 Elf64_Ehdr ehdr;
11 Elf64_Phdr *phdr;
12 Elf64_Shdr *shdr;
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.
21 Elf64_Rela **relocs;
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.
28 struct buffer **strtabs;
29 /* Parsed symbols. */
30 Elf64_Sym *syms;
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)
39 #define ELF_PARSE_ALL (-1)
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.
47 int parse_elf(const struct buffer *pinput, struct parsed_elf *pelf, int flags);
50 * Clean up memory associated with parsed_elf.
52 void parsed_elf_destroy(struct parsed_elf *pelf);
55 int
56 elf_headers(const struct buffer *pinput,
57 Elf64_Ehdr *ehdr,
58 Elf64_Phdr **pphdr,
59 Elf64_Shdr **pshdr);
61 /* ELF writing support. */
62 struct elf_writer;
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.
73 void elf_init_eheader(Elf64_Ehdr *ehdr, int machine, int nbits, int endian);
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.
80 struct elf_writer *elf_writer_init(const Elf64_Ehdr *ehdr);
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.
88 void elf_writer_destroy(struct elf_writer *ew);
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.
96 int elf_writer_add_section(struct elf_writer *ew, const Elf64_Shdr *shdr,
97 struct buffer *contents, const char *name);
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);
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);
111 * Serialize the ELF file to the output buffer. Return < 0 on error,
112 * 0 on success.
114 int elf_writer_serialize(struct elf_writer *ew, struct buffer *out);
117 * Calculate the loadable program's file size footprint. Returns < 0 on error,
118 * 0 on success.
120 int elf_program_file_size(const struct buffer *input, size_t *file_size);
122 #endif /* ELFPARSING_H */