[syslinux] [PATCH 3/4] efi: Useless relocations in PE file

Celelibi celelibi at gmail.com
Wed Nov 27 05:38:30 PST 2013


There is no need to have a relocation section that nothing points at.
The image is still seen as relocatable as long as the Characteristics of
the FileHeader do not say otherwise.

Moreover, the field base_relocation_table wasn't initialized properly
leading to unpredictable bugs.

Signed-off-by: Celelibi <celelibi at gmail.com>
---

I'm not 100% positive about the uselessness of the relocation section. However:
1) it works on my real hardware;
2) OVMF doesn't check the section headers at all;
3) all the docs I could find say that the section names are arbitrary;
4) the only way to make the relocation actually happen is by setting the
base_relocation_table field of the extra headers to point to this section;
5) The right way to indicate the image is not relocatable would be by setting
the characteristics IMAGE_FILE_RELOCS_STRIPPED in the coff header.

Moreover, the relocation entry cannot be inserted before the end of the headers
(512 bytes), this mean we would have to push everything a few bytes further.
And I think it's better if it can be avoided. :)

 efi/wrapper.c | 28 +++-------------------------
 1 file changed, 3 insertions(+), 25 deletions(-)

diff --git a/efi/wrapper.c b/efi/wrapper.c
index bd2c175..9652368 100644
--- a/efi/wrapper.c
+++ b/efi/wrapper.c
@@ -48,14 +48,12 @@ static void write_header(FILE *f, __uint32_t entry, size_t data_size,
 {
 	struct optional_hdr o_hdr;
 	struct optional_hdr_pe32p o_hdr_pe32p;
-	struct section t_sec, r_sec;
+	struct section t_sec;
 	struct extra_hdr e_hdr;
 	struct extra_hdr_pe32p e_hdr_pe32p;
 	struct coff_hdr c_hdr;
 	struct header hdr;
-	struct coff_reloc c_rel;
 	__uint32_t total_sz = data_size;
-	__uint32_t dummy = 0;
 	__uint32_t hdr_sz;
 	__uint32_t reloc_start, reloc_end;
 
@@ -78,7 +76,7 @@ static void write_header(FILE *f, __uint32_t entry, size_t data_size,
 	fwrite(&hdr, sizeof(hdr), 1, f);
 
 	memset(&c_hdr, 0, sizeof(c_hdr));
-	c_hdr.nr_sections = 2;
+	c_hdr.nr_sections = 1;
 	c_hdr.nr_syms = 1;
 	if (class == ELFCLASS32) {
 		c_hdr.arch = IMAGE_FILE_MACHINE_I386;
@@ -118,7 +116,7 @@ static void write_header(FILE *f, __uint32_t entry, size_t data_size,
 		o_hdr_pe32p.entry_point = entry;
 		o_hdr.initialized_data_sz = data_size;
 		fwrite(&o_hdr_pe32p, sizeof(o_hdr_pe32p), 1, f);
-		memset(&e_hdr_pe32p, 0, sizeof(e_hdr));
+		memset(&e_hdr_pe32p, 0, sizeof(e_hdr_pe32p));
 		e_hdr_pe32p.section_align = 4096;
 		e_hdr_pe32p.file_align = 512;
 		e_hdr_pe32p.image_sz = hdr_sz + so_size;
@@ -140,26 +138,6 @@ static void write_header(FILE *f, __uint32_t entry, size_t data_size,
 	fwrite(&t_sec, sizeof(t_sec), 1, f);
 
 	/*
-	 * Write our dummy relocation and reloc section.
-	 */
-	memset(&r_sec, 0, sizeof(r_sec));
-	strcpy((char *)r_sec.name, ".reloc");
-	r_sec.virtual_sz = sizeof(c_rel);
-	r_sec.virtual_address = ftell(f) + sizeof(r_sec);
-	r_sec.raw_data_sz = r_sec.virtual_sz;
-	r_sec.raw_data = r_sec.virtual_address;
-	r_sec.characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA |
-		IMAGE_SCN_ALIGN_1BYTES | IMAGE_SCN_MEM_DISCARDABLE |
-		IMAGE_SCN_MEM_READ;
-	fwrite(&r_sec, sizeof(r_sec), 1, f);
-
-	memset(&c_rel, 0, sizeof(c_rel));
-	c_rel.virtual_address = ftell(f) + sizeof(c_rel);
-	c_rel.symtab_index = 10;
-	fwrite(&c_rel, sizeof(c_rel), 1, f);
-	fwrite(&dummy, sizeof(dummy), 1, f);
-
-	/*
 	 * Add some padding to align the ELF as needed
 	 */
 	if (ftell(f) > t_sec.virtual_address) {
-- 
1.8.4.3



More information about the Syslinux mailing list