1 /* SPDX-License-Identifier: GPL-2.0-only */
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
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
28 struct buffer
**strtabs
;
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
);
56 elf_headers(const struct buffer
*pinput
,
61 /* ELF writing support. */
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
80 struct elf_writer
*elf_writer_init(const Elf64_Ehdr
*ehdr
);
83 * Clean up any internal state represented by ew. Aftewards the elf_writer
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,
114 int elf_writer_serialize(struct elf_writer
*ew
, struct buffer
*out
);
117 * Calculate the loadable program's file size footprint and alignment requirements.
118 * Returns < 0 on error, 0 on success.
120 int elf_program_file_size_align(const struct buffer
*input
, size_t *file_size
, size_t *align
);
122 #endif /* ELFPARSING_H */