Skip to content

Commit efa089e

Browse files
ardbiesheuvelIngo Molnar
authored and
Ingo Molnar
committed
x86/boot: Construct PE/COFF .text section from assembler
Now that the size of the setup block is visible to the assembler, it is possible to populate the PE/COFF header fields from the asm code directly, instead of poking the values into the binary using the build tool. This will make it easier to reorganize the section layout without having to tweak the build tool in lockstep. This change has no impact on the resulting bzImage binary. Signed-off-by: Ard Biesheuvel <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent aeb9206 commit efa089e

File tree

2 files changed

+7
-62
lines changed

2 files changed

+7
-62
lines changed

arch/x86/boot/header.S

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,12 @@ optional_header:
7575
.byte 0x02 # MajorLinkerVersion
7676
.byte 0x14 # MinorLinkerVersion
7777

78-
# Filled in by build.c
79-
.long 0 # SizeOfCode
78+
.long setup_size + ZO__end - 0x200 # SizeOfCode
8079

8180
.long 0 # SizeOfInitializedData
8281
.long 0 # SizeOfUninitializedData
8382

84-
# Filled in by build.c
85-
.long 0x0000 # AddressOfEntryPoint
83+
.long setup_size + ZO_efi_pe_entry # AddressOfEntryPoint
8684

8785
.long 0x0200 # BaseOfCode
8886
#ifdef CONFIG_X86_32
@@ -105,10 +103,7 @@ extra_header_fields:
105103
.word 0 # MinorSubsystemVersion
106104
.long 0 # Win32VersionValue
107105

108-
#
109-
# The size of the bzImage is written in tools/build.c
110-
#
111-
.long 0 # SizeOfImage
106+
.long setup_size + ZO__end # SizeOfImage
112107

113108
.long 0x200 # SizeOfHeaders
114109
.long 0 # CheckSum
@@ -199,18 +194,15 @@ section_table:
199194
IMAGE_SCN_MEM_DISCARDABLE # Characteristics
200195
#endif
201196

202-
#
203-
# The offset & size fields are filled in by build.c.
204-
#
205197
.ascii ".text"
206198
.byte 0
207199
.byte 0
208200
.byte 0
209-
.long 0
210-
.long 0x0 # startup_{32,64}
211-
.long 0 # Size of initialized data
201+
.long ZO__end
202+
.long setup_size
203+
.long ZO__edata # Size of initialized data
212204
# on disk
213-
.long 0x0 # startup_{32,64}
205+
.long setup_size
214206
.long 0 # PointerToRelocations
215207
.long 0 # PointerToLineNumbers
216208
.word 0 # NumberOfRelocations

arch/x86/boot/tools/build.c

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,8 @@ u8 buf[SETUP_SECT_MAX*512];
5050
#define PECOFF_RELOC_RESERVE 0x20
5151
#define PECOFF_COMPAT_RESERVE 0x20
5252

53-
static unsigned long efi_pe_entry;
5453
static unsigned long efi32_pe_entry;
5554
static unsigned long _edata;
56-
static unsigned long _end;
5755

5856
/*----------------------------------------------------------------------*/
5957

@@ -216,55 +214,16 @@ static void update_pecoff_setup_and_reloc(unsigned int size)
216214
#endif
217215
}
218216

219-
static void update_pecoff_text(unsigned int text_start, unsigned int file_sz)
220-
{
221-
unsigned int pe_header;
222-
unsigned int text_sz = file_sz - text_start;
223-
unsigned int bss_sz = _end - text_sz;
224-
225-
pe_header = get_unaligned_le32(&buf[0x3c]);
226-
227-
/*
228-
* Size of code: Subtract the size of the first sector (512 bytes)
229-
* which includes the header.
230-
*/
231-
put_unaligned_le32(file_sz - 512 + bss_sz, &buf[pe_header + 0x1c]);
232-
233-
/* Size of image */
234-
put_unaligned_le32(file_sz + bss_sz, &buf[pe_header + 0x50]);
235-
236-
/*
237-
* Address of entry point for PE/COFF executable
238-
*/
239-
put_unaligned_le32(text_start + efi_pe_entry, &buf[pe_header + 0x28]);
240-
241-
update_pecoff_section_header_fields(".text", text_start, text_sz + bss_sz,
242-
text_sz, text_start);
243-
}
244-
245217
static int reserve_pecoff_reloc_section(int c)
246218
{
247219
/* Reserve 0x20 bytes for .reloc section */
248220
memset(buf+c, 0, PECOFF_RELOC_RESERVE);
249221
return PECOFF_RELOC_RESERVE;
250222
}
251223

252-
static void efi_stub_defaults(void)
253-
{
254-
/* Defaults for old kernel */
255-
#ifdef CONFIG_X86_32
256-
efi_pe_entry = 0x10;
257-
#else
258-
efi_pe_entry = 0x210;
259-
#endif
260-
}
261-
262224
#else
263225

264226
static inline void update_pecoff_setup_and_reloc(unsigned int size) {}
265-
static inline void update_pecoff_text(unsigned int text_start,
266-
unsigned int file_sz) {}
267-
static inline void efi_stub_defaults(void) {}
268227

269228
static inline int reserve_pecoff_reloc_section(int c)
270229
{
@@ -307,10 +266,8 @@ static void parse_zoffset(char *fname)
307266
p = (char *)buf;
308267

309268
while (p && *p) {
310-
PARSE_ZOFS(p, efi_pe_entry);
311269
PARSE_ZOFS(p, efi32_pe_entry);
312270
PARSE_ZOFS(p, _edata);
313-
PARSE_ZOFS(p, _end);
314271

315272
p = strchr(p, '\n');
316273
while (p && (*p == '\r' || *p == '\n'))
@@ -328,8 +285,6 @@ int main(int argc, char ** argv)
328285
void *kernel;
329286
u32 crc = 0xffffffffUL;
330287

331-
efi_stub_defaults();
332-
333288
if (argc != 5)
334289
usage();
335290
parse_zoffset(argv[3]);
@@ -376,8 +331,6 @@ int main(int argc, char ** argv)
376331
kernel = mmap(NULL, sz, PROT_READ, MAP_SHARED, fd, 0);
377332
if (kernel == MAP_FAILED)
378333
die("Unable to mmap '%s': %m", argv[2]);
379-
update_pecoff_text(setup_sectors * 512, i + _edata);
380-
381334

382335
crc = partial_crc32(buf, i, crc);
383336
if (fwrite(buf, 1, i, dest) != i)

0 commit comments

Comments
 (0)