[coreboot-gerrit] New patch to review for coreboot: cbfstool: add ELF header initialization helper

Aaron Durbin (adurbin@chromium.org) gerrit at coreboot.org
Wed Oct 28 17:44:42 CET 2015


Aaron Durbin (adurbin at chromium.org) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/12218

-gerrit

commit 2ee16b5da94b921a3d5346653be7e363539f93b3
Author: Aaron Durbin <adurbin at chromium.org>
Date:   Tue Oct 27 16:21:55 2015 -0500

    cbfstool: add ELF header initialization helper
    
    In order for one to extract ELF files from cbfs it's
    helpful to have common code which creates a default
    executable ELF header for the provided constraints.
    
    BUG=None
    TEST=With follow up patch am able to extract out romstage
         as an ELF file.
    
    Change-Id: Ib8f2456f41b79c6c0430861e33e8b909725013f1
    Signed-off-by: Aaron Durbin <adurbin at chromium.org>
---
 util/cbfstool/elfheaders.c | 24 ++++++++++++++++++++++++
 util/cbfstool/elfparsing.h | 10 ++++++++++
 2 files changed, 34 insertions(+)

diff --git a/util/cbfstool/elfheaders.c b/util/cbfstool/elfheaders.c
index 4cf3534..da11bfc 100644
--- a/util/cbfstool/elfheaders.c
+++ b/util/cbfstool/elfheaders.c
@@ -635,6 +635,30 @@ elf_headers(const struct buffer *pinput,
  * +------------------+
  */
 
+void elf_init_eheader(Elf64_Ehdr *ehdr, int machine, int nbits, int endian)
+{
+	memset(ehdr, 0, sizeof(*ehdr));
+	ehdr->e_ident[EI_MAG0] = ELFMAG0;
+	ehdr->e_ident[EI_MAG1] = ELFMAG1;
+	ehdr->e_ident[EI_MAG2] = ELFMAG2;
+	ehdr->e_ident[EI_MAG3] = ELFMAG3;
+	ehdr->e_ident[EI_CLASS] = nbits;
+	ehdr->e_ident[EI_DATA] = endian;
+	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
+	ehdr->e_type = ET_EXEC;
+	ehdr->e_machine = machine;
+	ehdr->e_version = EV_CURRENT;
+	if (nbits == ELFCLASS64) {
+		ehdr->e_ehsize = sizeof(Elf64_Ehdr);
+		ehdr->e_phentsize = sizeof(Elf64_Phdr);
+		ehdr->e_shentsize = sizeof(Elf64_Shdr);
+	} else {
+		ehdr->e_ehsize = sizeof(Elf32_Ehdr);
+		ehdr->e_phentsize = sizeof(Elf32_Phdr);
+		ehdr->e_shentsize = sizeof(Elf32_Shdr);
+	}
+}
+
 /* Arbitray maximum number of sections. */
 #define MAX_SECTIONS 16
 struct elf_writer_section {
diff --git a/util/cbfstool/elfparsing.h b/util/cbfstool/elfparsing.h
index f7d3c23..e2b3f03 100644
--- a/util/cbfstool/elfparsing.h
+++ b/util/cbfstool/elfparsing.h
@@ -77,6 +77,16 @@ elf_headers(const struct buffer *pinput,
 struct elf_writer;
 
 /*
+ * Initialize a 64-bit ELF header provided the inputs. While the structure
+ * is a 64-bit header one can specify a 32-bit machine. The 64-bit version
+ * is just used as a common structure. If one wants to specify the entry
+ * point, for example, the caller can set it after filling in the common
+ * bits. The machine, nbits, and endian values should be from the ELF
+ * defintions (e.g. EM_386, ELFCLASS32, and ELFDATA2LSB).
+ */
+void elf_init_eheader(Elf64_Ehdr *ehdr, int machine, int nbits, int endian);
+
+/*
  * Initialize a new ELF writer. Deafult machine type, endianness, etc is
  * copied from the passed in Elf64_Ehdr. Returns NULL on failure, valid
  * pointer on success.



More information about the coreboot-gerrit mailing list