syslinux-6.02-fix-efi-ovmf.patch 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. The value of the field rva_and_sizes_nr is used by OVMF to check the
  2. consistency of the PE file with respect to the field optional_hdr_sz. It
  3. now have the right value.
  4. Signed-off-by: Celelibi <celelibi at gmail.com>
  5. ---
  6. efi/wrapper.c | 6 +++---
  7. efi/wrapper.h | 28 ++++++++++++++++------------
  8. 2 files changed, 19 insertions(+), 15 deletions(-)
  9. diff --git a/efi/wrapper.c b/efi/wrapper.c
  10. index 04c895f..ec77271 100644
  11. --- a/efi/wrapper.c
  12. +++ b/efi/wrapper.c
  13. @@ -102,7 +102,7 @@ static void write_header(FILE *f, __uint32_t entry, size_t data_size,
  14. e_hdr.image_sz = total_sz;
  15. e_hdr.headers_sz = 512;
  16. e_hdr.subsystem = IMAGE_SUBSYSTEM_EFI_APPLICATION;
  17. - e_hdr.rva_and_sizes_nr = 1;
  18. + e_hdr.rva_and_sizes_nr = sizeof(e_hdr.data_directory) / sizeof(__uint64_t);
  19. fwrite(&e_hdr, sizeof(e_hdr), 1, f);
  20. }
  21. else if (class == ELFCLASS64) {
  22. @@ -130,7 +130,7 @@ static void write_header(FILE *f, __uint32_t entry, size_t data_size,
  23. e_hdr_pe32p.image_sz = total_sz;
  24. e_hdr_pe32p.headers_sz = 512;
  25. e_hdr_pe32p.subsystem = IMAGE_SUBSYSTEM_EFI_APPLICATION;
  26. - e_hdr_pe32p.rva_and_sizes_nr = 1;
  27. + e_hdr_pe32p.rva_and_sizes_nr = sizeof(e_hdr_pe32p.data_directory) / sizeof(__uint64_t);
  28. fwrite(&e_hdr_pe32p, sizeof(e_hdr_pe32p), 1, f);
  29. }
  30. @@ -237,7 +237,7 @@ int main(int argc, char **argv)
  31. fprintf(stderr, "Unsupported architecture\n");
  32. exit(EXIT_FAILURE);
  33. }
  34. -
  35. +
  36. if (id[EI_MAG0] != ELFMAG0 ||
  37. id[EI_MAG1] != ELFMAG1 ||
  38. id[EI_MAG2] != ELFMAG2 ||
  39. diff --git a/efi/wrapper.h b/efi/wrapper.h
  40. index 4f76991..0e6b38e 100644
  41. --- a/efi/wrapper.h
  42. +++ b/efi/wrapper.h
  43. @@ -102,12 +102,14 @@ struct extra_hdr {
  44. __uint32_t heap_commit_sz;
  45. __uint32_t loader_flags;
  46. __uint32_t rva_and_sizes_nr;
  47. - __uint64_t export_table;
  48. - __uint64_t import_table;
  49. - __uint64_t resource_table;
  50. - __uint64_t exception_table;
  51. - __uint64_t certification_table;
  52. - __uint64_t base_relocation_table;
  53. + struct {
  54. + __uint64_t export_table;
  55. + __uint64_t import_table;
  56. + __uint64_t resource_table;
  57. + __uint64_t exception_table;
  58. + __uint64_t certification_table;
  59. + __uint64_t base_relocation_table;
  60. + } data_directory;
  61. } __packed;
  62. /* Extra header for PE32+ format
  63. @@ -136,12 +138,14 @@ struct extra_hdr_pe32p {
  64. __uint64_t heap_commit_sz;
  65. __uint32_t loader_flags;
  66. __uint32_t rva_and_sizes_nr;
  67. - __uint64_t export_table;
  68. - __uint64_t import_table;
  69. - __uint64_t resource_table;
  70. - __uint64_t exception_table;
  71. - __uint64_t certification_table;
  72. - __uint64_t base_relocation_table;
  73. + struct {
  74. + __uint64_t export_table;
  75. + __uint64_t import_table;
  76. + __uint64_t resource_table;
  77. + __uint64_t exception_table;
  78. + __uint64_t certification_table;
  79. + __uint64_t base_relocation_table;
  80. + } data_directory;
  81. } __packed;
  82. struct section {
  83. In the generated PE file, the section header for the .text section used
  84. to address more than the whole file. Starting at offset 0 (before the
  85. end of the headers) is illegal and is rejected by OVMF. Giving a size
  86. greater than the actual file size is also illegal and rejected.
  87. Moreover, the actual data inside the PE file have to be aligned to at
  88. least 512 bytes. Hence, .text need to be aligned as well.
  89. Signed-off-by: Celelibi <celelibi at gmail.com>
  90. ---
  91. efi/wrapper.c | 44 +++++++++++++++++++++++++-------------------
  92. 1 file changed, 25 insertions(+), 19 deletions(-)
  93. diff --git a/efi/wrapper.c b/efi/wrapper.c
  94. index ec77271..bd2c175 100644
  95. --- a/efi/wrapper.c
  96. +++ b/efi/wrapper.c
  97. @@ -54,11 +54,15 @@ static void write_header(FILE *f, __uint32_t entry, size_t data_size,
  98. struct coff_hdr c_hdr;
  99. struct header hdr;
  100. struct coff_reloc c_rel;
  101. - __uint32_t total_sz = so_size;
  102. + __uint32_t total_sz = data_size;
  103. __uint32_t dummy = 0;
  104. __uint32_t hdr_sz;
  105. __uint32_t reloc_start, reloc_end;
  106. + hdr_sz = 512;
  107. + total_sz += hdr_sz;
  108. + entry += hdr_sz;
  109. +
  110. memset(&hdr, 0, sizeof(hdr));
  111. hdr.msdos_signature = MSDOS_SIGNATURE;
  112. @@ -77,11 +81,6 @@ static void write_header(FILE *f, __uint32_t entry, size_t data_size,
  113. c_hdr.nr_sections = 2;
  114. c_hdr.nr_syms = 1;
  115. if (class == ELFCLASS32) {
  116. - hdr_sz = sizeof(o_hdr) + sizeof(t_sec) + sizeof(e_hdr) +
  117. - sizeof(r_sec) + sizeof(c_hdr) + sizeof(hdr) + sizeof(c_rel)
  118. - + sizeof(dummy);
  119. - total_sz += hdr_sz;
  120. - entry += hdr_sz;
  121. c_hdr.arch = IMAGE_FILE_MACHINE_I386;
  122. c_hdr.characteristics = IMAGE_FILE_32BIT_MACHINE |
  123. IMAGE_FILE_DEBUG_STRIPPED | IMAGE_FILE_EXECUTABLE_IMAGE |
  124. @@ -92,25 +91,20 @@ static void write_header(FILE *f, __uint32_t entry, size_t data_size,
  125. o_hdr.format = PE32_FORMAT;
  126. o_hdr.major_linker_version = 0x02;
  127. o_hdr.minor_linker_version = 0x14;
  128. - o_hdr.code_sz = total_sz;
  129. + o_hdr.code_sz = data_size;
  130. o_hdr.entry_point = entry;
  131. o_hdr.initialized_data_sz = data_size;
  132. fwrite(&o_hdr, sizeof(o_hdr), 1, f);
  133. memset(&e_hdr, 0, sizeof(e_hdr));
  134. e_hdr.section_align = 4096;
  135. e_hdr.file_align = 512;
  136. - e_hdr.image_sz = total_sz;
  137. - e_hdr.headers_sz = 512;
  138. + e_hdr.image_sz = hdr_sz + so_size;
  139. + e_hdr.headers_sz = hdr_sz;
  140. e_hdr.subsystem = IMAGE_SUBSYSTEM_EFI_APPLICATION;
  141. e_hdr.rva_and_sizes_nr = sizeof(e_hdr.data_directory) / sizeof(__uint64_t);
  142. fwrite(&e_hdr, sizeof(e_hdr), 1, f);
  143. }
  144. else if (class == ELFCLASS64) {
  145. - hdr_sz = sizeof(o_hdr_pe32p) + sizeof(t_sec) + sizeof(e_hdr_pe32p) +
  146. - sizeof(r_sec) + sizeof(c_hdr) + sizeof(hdr) + sizeof(c_rel)
  147. - + sizeof(dummy);
  148. - total_sz += hdr_sz;
  149. - entry += hdr_sz;
  150. c_hdr.arch = IMAGE_FILE_MACHINE_X86_64;
  151. c_hdr.characteristics = IMAGE_FILE_DEBUG_STRIPPED | IMAGE_FILE_EXECUTABLE_IMAGE |
  152. IMAGE_FILE_LINE_NUMBERS_STRIPPED;
  153. @@ -120,15 +114,15 @@ static void write_header(FILE *f, __uint32_t entry, size_t data_size,
  154. o_hdr_pe32p.format = PE32P_FORMAT;
  155. o_hdr_pe32p.major_linker_version = 0x02;
  156. o_hdr_pe32p.minor_linker_version = 0x14;
  157. - o_hdr_pe32p.code_sz = total_sz;
  158. + o_hdr_pe32p.code_sz = data_size;
  159. o_hdr_pe32p.entry_point = entry;
  160. o_hdr.initialized_data_sz = data_size;
  161. fwrite(&o_hdr_pe32p, sizeof(o_hdr_pe32p), 1, f);
  162. memset(&e_hdr_pe32p, 0, sizeof(e_hdr));
  163. e_hdr_pe32p.section_align = 4096;
  164. e_hdr_pe32p.file_align = 512;
  165. - e_hdr_pe32p.image_sz = total_sz;
  166. - e_hdr_pe32p.headers_sz = 512;
  167. + e_hdr_pe32p.image_sz = hdr_sz + so_size;
  168. + e_hdr_pe32p.headers_sz = hdr_sz;
  169. e_hdr_pe32p.subsystem = IMAGE_SUBSYSTEM_EFI_APPLICATION;
  170. e_hdr_pe32p.rva_and_sizes_nr = sizeof(e_hdr_pe32p.data_directory) / sizeof(__uint64_t);
  171. fwrite(&e_hdr_pe32p, sizeof(e_hdr_pe32p), 1, f);
  172. @@ -136,8 +130,10 @@ static void write_header(FILE *f, __uint32_t entry, size_t data_size,
  173. memset(&t_sec, 0, sizeof(t_sec));
  174. strcpy((char *)t_sec.name, ".text");
  175. - t_sec.virtual_sz = total_sz;
  176. - t_sec.raw_data_sz = total_sz;
  177. + t_sec.virtual_sz = data_size;
  178. + t_sec.virtual_address = hdr_sz;
  179. + t_sec.raw_data_sz = t_sec.virtual_sz;
  180. + t_sec.raw_data = t_sec.virtual_address;
  181. t_sec.characteristics = IMAGE_SCN_CNT_CODE |
  182. IMAGE_SCN_ALIGN_16BYTES | IMAGE_SCN_MEM_EXECUTE |
  183. IMAGE_SCN_MEM_READ;
  184. @@ -163,6 +159,16 @@ static void write_header(FILE *f, __uint32_t entry, size_t data_size,
  185. fwrite(&c_rel, sizeof(c_rel), 1, f);
  186. fwrite(&dummy, sizeof(dummy), 1, f);
  187. + /*
  188. + * Add some padding to align the ELF as needed
  189. + */
  190. + if (ftell(f) > t_sec.virtual_address) {
  191. + // A fseek that rewind would be a bug hard to track
  192. + fprintf(stderr, "PE+ headers are too large.\n");
  193. + exit(EXIT_FAILURE);
  194. + }
  195. +
  196. + fseek(f, t_sec.virtual_address, SEEK_SET);
  197. }
  198. static void usage(char *progname)
  199. There is no need to have a relocation section that nothing points at.
  200. The image is still seen as relocatable as long as the Characteristics of
  201. the FileHeader do not say otherwise.
  202. Moreover, the field base_relocation_table wasn't initialized properly
  203. leading to unpredictable bugs.
  204. Signed-off-by: Celelibi <celelibi at gmail.com>
  205. ---
  206. I'm not 100% positive about the uselessness of the relocation section. However:
  207. 1) it works on my real hardware;
  208. 2) OVMF doesn't check the section headers at all;
  209. 3) all the docs I could find say that the section names are arbitrary;
  210. 4) the only way to make the relocation actually happen is by setting the
  211. base_relocation_table field of the extra headers to point to this section;
  212. 5) The right way to indicate the image is not relocatable would be by setting
  213. the characteristics IMAGE_FILE_RELOCS_STRIPPED in the coff header.
  214. Moreover, the relocation entry cannot be inserted before the end of the headers
  215. (512 bytes), this mean we would have to push everything a few bytes further.
  216. And I think it's better if it can be avoided. :)
  217. efi/wrapper.c | 28 +++-------------------------
  218. 1 file changed, 3 insertions(+), 25 deletions(-)
  219. diff --git a/efi/wrapper.c b/efi/wrapper.c
  220. index bd2c175..9652368 100644
  221. --- a/efi/wrapper.c
  222. +++ b/efi/wrapper.c
  223. @@ -48,14 +48,12 @@ static void write_header(FILE *f, __uint32_t entry, size_t data_size,
  224. {
  225. struct optional_hdr o_hdr;
  226. struct optional_hdr_pe32p o_hdr_pe32p;
  227. - struct section t_sec, r_sec;
  228. + struct section t_sec;
  229. struct extra_hdr e_hdr;
  230. struct extra_hdr_pe32p e_hdr_pe32p;
  231. struct coff_hdr c_hdr;
  232. struct header hdr;
  233. - struct coff_reloc c_rel;
  234. __uint32_t total_sz = data_size;
  235. - __uint32_t dummy = 0;
  236. __uint32_t hdr_sz;
  237. __uint32_t reloc_start, reloc_end;
  238. @@ -78,7 +76,7 @@ static void write_header(FILE *f, __uint32_t entry, size_t data_size,
  239. fwrite(&hdr, sizeof(hdr), 1, f);
  240. memset(&c_hdr, 0, sizeof(c_hdr));
  241. - c_hdr.nr_sections = 2;
  242. + c_hdr.nr_sections = 1;
  243. c_hdr.nr_syms = 1;
  244. if (class == ELFCLASS32) {
  245. c_hdr.arch = IMAGE_FILE_MACHINE_I386;
  246. @@ -118,7 +116,7 @@ static void write_header(FILE *f, __uint32_t entry, size_t data_size,
  247. o_hdr_pe32p.entry_point = entry;
  248. o_hdr.initialized_data_sz = data_size;
  249. fwrite(&o_hdr_pe32p, sizeof(o_hdr_pe32p), 1, f);
  250. - memset(&e_hdr_pe32p, 0, sizeof(e_hdr));
  251. + memset(&e_hdr_pe32p, 0, sizeof(e_hdr_pe32p));
  252. e_hdr_pe32p.section_align = 4096;
  253. e_hdr_pe32p.file_align = 512;
  254. e_hdr_pe32p.image_sz = hdr_sz + so_size;
  255. @@ -140,26 +138,6 @@ static void write_header(FILE *f, __uint32_t entry, size_t data_size,
  256. fwrite(&t_sec, sizeof(t_sec), 1, f);
  257. /*
  258. - * Write our dummy relocation and reloc section.
  259. - */
  260. - memset(&r_sec, 0, sizeof(r_sec));
  261. - strcpy((char *)r_sec.name, ".reloc");
  262. - r_sec.virtual_sz = sizeof(c_rel);
  263. - r_sec.virtual_address = ftell(f) + sizeof(r_sec);
  264. - r_sec.raw_data_sz = r_sec.virtual_sz;
  265. - r_sec.raw_data = r_sec.virtual_address;
  266. - r_sec.characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA |
  267. - IMAGE_SCN_ALIGN_1BYTES | IMAGE_SCN_MEM_DISCARDABLE |
  268. - IMAGE_SCN_MEM_READ;
  269. - fwrite(&r_sec, sizeof(r_sec), 1, f);
  270. -
  271. - memset(&c_rel, 0, sizeof(c_rel));
  272. - c_rel.virtual_address = ftell(f) + sizeof(c_rel);
  273. - c_rel.symtab_index = 10;
  274. - fwrite(&c_rel, sizeof(c_rel), 1, f);
  275. - fwrite(&dummy, sizeof(dummy), 1, f);
  276. -
  277. - /*
  278. * Add some padding to align the ELF as needed
  279. */
  280. if (ftell(f) > t_sec.virtual_address) {
  281. PE headers code_sz and image_sz indicate more or less, the size of the
  282. file and the size of the in-memory image. They are now given the right
  283. value.
  284. In the ELF format, only the program headers are reliable to determine
  285. the actually needed part of the file and the in-memory size.
  286. The .bss section should always be marked as NOLOAD for ld since its
  287. content shouldn't be included into the binary file.
  288. Signed-off-by: Celelibi <celelibi at gmail.com>
  289. ---
  290. Again, I'm not 100% positive about the semantics of NOLOAD in a linker script.
  291. However I found this make things work better.
  292. Actually the file core/fs/fs.o have a .bss16 section that is not empty. If I
  293. understood correctly, only .bss can be empty in a .o. Thus ld generates the
  294. zeros that belong to that section in the .o. Then, when merging the section
  295. .bss16 into the .bss of syslinux.so, ld is forced to generate ALL the zeros
  296. that belong to the .bss section. Marking this section as NOLOAD make ld to
  297. ignore its content but still produce a PT_LOAD program header with the right
  298. memory size, as needed for the bss.
  299. efi/i386/syslinux.ld | 4 +-
  300. efi/wrapper.c | 138 +++++++++++++++----------------------------------
  301. efi/x86_64/syslinux.ld | 4 +-
  302. 3 files changed, 45 insertions(+), 101 deletions(-)
  303. diff --git a/efi/i386/syslinux.ld b/efi/i386/syslinux.ld
  304. index 523a9b9..bab3fc7 100644
  305. --- a/efi/i386/syslinux.ld
  306. +++ b/efi/i386/syslinux.ld
  307. @@ -136,7 +136,7 @@ SECTIONS
  308. *(.strtab)
  309. }
  310. - .bss : {
  311. + .bss (NOLOAD) : {
  312. /* the EFI loader doesn't seem to like a .bss section,
  313. so we stick it all into .data: */
  314. __bss_start = .;
  315. @@ -153,7 +153,7 @@ SECTIONS
  316. __bss_dwords = (__bss_len + 3) >> 2;
  317. . = ALIGN(128);
  318. -
  319. +
  320. /* Very large objects which don't need to be zeroed */
  321. .hugebss : {
  322. diff --git a/efi/wrapper.c b/efi/wrapper.c
  323. index 9652368..a5247ae 100644
  324. --- a/efi/wrapper.c
  325. +++ b/efi/wrapper.c
  326. @@ -35,7 +35,7 @@ typedef Elf64_Addr Elf_Addr;
  327. #endif
  328. /*
  329. - * 'so_size' is the file size of the ELF shared object.
  330. + * 'so_memsz' is the size of the ELF shared object once loaded.
  331. * 'data_size' is the size of initialised data in the shared object.
  332. * 'class' dictates how the header is written
  333. * For 32bit machines (class == ELFCLASS32), the optional
  334. @@ -44,7 +44,7 @@ typedef Elf64_Addr Elf_Addr;
  335. * header includes PE32+header fields
  336. */
  337. static void write_header(FILE *f, __uint32_t entry, size_t data_size,
  338. - __uint32_t so_size, __uint8_t class)
  339. + __uint32_t so_memsz, __uint8_t class)
  340. {
  341. struct optional_hdr o_hdr;
  342. struct optional_hdr_pe32p o_hdr_pe32p;
  343. @@ -96,7 +96,7 @@ static void write_header(FILE *f, __uint32_t entry, size_t data_size,
  344. memset(&e_hdr, 0, sizeof(e_hdr));
  345. e_hdr.section_align = 4096;
  346. e_hdr.file_align = 512;
  347. - e_hdr.image_sz = hdr_sz + so_size;
  348. + e_hdr.image_sz = hdr_sz + so_memsz;
  349. e_hdr.headers_sz = hdr_sz;
  350. e_hdr.subsystem = IMAGE_SUBSYSTEM_EFI_APPLICATION;
  351. e_hdr.rva_and_sizes_nr = sizeof(e_hdr.data_directory) / sizeof(__uint64_t);
  352. @@ -119,7 +119,7 @@ static void write_header(FILE *f, __uint32_t entry, size_t data_size,
  353. memset(&e_hdr_pe32p, 0, sizeof(e_hdr_pe32p));
  354. e_hdr_pe32p.section_align = 4096;
  355. e_hdr_pe32p.file_align = 512;
  356. - e_hdr_pe32p.image_sz = hdr_sz + so_size;
  357. + e_hdr_pe32p.image_sz = hdr_sz + so_memsz;
  358. e_hdr_pe32p.headers_sz = hdr_sz;
  359. e_hdr_pe32p.subsystem = IMAGE_SUBSYSTEM_EFI_APPLICATION;
  360. e_hdr_pe32p.rva_and_sizes_nr = sizeof(e_hdr_pe32p.data_directory) / sizeof(__uint64_t);
  361. @@ -157,17 +157,16 @@ static void usage(char *progname)
  362. int main(int argc, char **argv)
  363. {
  364. - struct stat st;
  365. Elf32_Ehdr e32_hdr;
  366. Elf64_Ehdr e64_hdr;
  367. __uint32_t entry;
  368. __uint8_t class;
  369. - __uint64_t shoff;
  370. - __uint16_t shnum, shentsize, shstrndx;
  371. + __uint64_t phoff = 0;
  372. + __uint16_t phnum = 0, phentsize = 0;
  373. unsigned char *id;
  374. FILE *f_in, *f_out;
  375. void *buf;
  376. - size_t datasz, rv;
  377. + size_t datasz, memsz, rv;
  378. if (argc < 3) {
  379. usage(argv[0]);
  380. @@ -180,11 +179,6 @@ int main(int argc, char **argv)
  381. exit(EXIT_FAILURE);
  382. }
  383. - if (stat(argv[1], &st) != 0) {
  384. - perror("stat");
  385. - exit(EXIT_FAILURE);
  386. - }
  387. -
  388. f_out = fopen(argv[2], "w");
  389. if (!f_out) {
  390. perror("fopen");
  391. @@ -194,15 +188,14 @@ int main(int argc, char **argv)
  392. /*
  393. * Parse the ELF header and find the entry point.
  394. */
  395. - fread((void *)&e32_hdr, sizeof(e32_hdr), 1, f_in);
  396. + fread((void *)&e32_hdr, sizeof(e32_hdr), 1, f_in);
  397. if (e32_hdr.e_ident[EI_CLASS] == ELFCLASS32) {
  398. id = e32_hdr.e_ident;
  399. class = ELFCLASS32;
  400. entry = e32_hdr.e_entry;
  401. - shoff = e32_hdr.e_shoff;
  402. - shnum = e32_hdr.e_shnum;
  403. - shstrndx = e32_hdr.e_shstrndx;
  404. - shentsize = e32_hdr.e_shentsize;
  405. + phoff = e32_hdr.e_phoff;
  406. + phnum = e32_hdr.e_phnum;
  407. + phentsize = e32_hdr.e_phentsize;
  408. }
  409. else if (e32_hdr.e_ident[EI_CLASS] == ELFCLASS64) {
  410. /* read the header again for x86_64
  411. @@ -213,10 +206,9 @@ int main(int argc, char **argv)
  412. fread((void *)&e64_hdr, sizeof(e64_hdr), 1, f_in);
  413. id = e64_hdr.e_ident;
  414. entry = e64_hdr.e_entry;
  415. - shoff = e64_hdr.e_shoff;
  416. - shnum = e64_hdr.e_shnum;
  417. - shstrndx = e64_hdr.e_shstrndx;
  418. - shentsize = e64_hdr.e_shentsize;
  419. + phoff = e64_hdr.e_phoff;
  420. + phnum = e64_hdr.e_phnum;
  421. + phentsize = e64_hdr.e_phentsize;
  422. } else {
  423. fprintf(stderr, "Unsupported architecture\n");
  424. exit(EXIT_FAILURE);
  425. @@ -230,98 +222,47 @@ int main(int argc, char **argv)
  426. exit(EXIT_FAILURE);
  427. }
  428. - if (!shoff || !shnum || (shstrndx == SHN_UNDEF)) {
  429. - fprintf(stderr, "Cannot find section table\n");
  430. + if (!phoff || !phnum) {
  431. + fprintf(stderr, "Cannot find segment table\n");
  432. exit(EXIT_FAILURE);
  433. }
  434. /*
  435. - * Find the beginning of the .bss section. Everything preceding
  436. - * it is copied verbatim to the output file.
  437. + * Find the LOAD program header. Everything in this segment
  438. + * is copied verbatim to the output file.
  439. + * Although there may be several LOAD program headers, only
  440. + * one is currently copied.
  441. */
  442. if (e32_hdr.e_ident[EI_CLASS] == ELFCLASS32) {
  443. - const char *shstrtab, *name;
  444. - Elf32_Shdr shdr;
  445. + Elf32_Phdr phdr;
  446. int i;
  447. - void *strtab;
  448. -
  449. - fseek(f_in, shoff, SEEK_SET);
  450. -
  451. - /* First find the strtab section */
  452. - fseek(f_in, shstrndx * shentsize, SEEK_CUR);
  453. - fread(&shdr, sizeof(shdr), 1, f_in);
  454. - strtab = malloc(shdr.sh_size);
  455. - if (!strtab) {
  456. - fprintf(stderr, "Failed to malloc strtab\n");
  457. - exit(EXIT_FAILURE);
  458. - }
  459. -
  460. - fseek(f_in, shdr.sh_offset, SEEK_SET);
  461. - fread(strtab, shdr.sh_size, 1, f_in);
  462. -
  463. - /* Now search for the .bss section */
  464. - fseek(f_in, shoff, SEEK_SET);
  465. - for (i = 0; i < shnum; i++) {
  466. - rv = fread(&shdr, sizeof(shdr), 1, f_in);
  467. - if (!rv) {
  468. - fprintf(stderr, "Failed to read section table\n");
  469. - exit(EXIT_FAILURE);
  470. - }
  471. + /* Find the first LOAD program header */
  472. + for (i = 0; i < phnum; i++) {
  473. + fseek(f_in, phoff + i * phentsize, SEEK_SET);
  474. + fread(&phdr, sizeof(phdr), 1, f_in);
  475. - name = strtab + shdr.sh_name;
  476. - if (!strcmp(name, ".bss"))
  477. + if (phdr.p_type == PT_LOAD)
  478. break;
  479. }
  480. - if (i == shnum) {
  481. - fprintf(stderr, "Failed to find .bss section\n");
  482. - exit(EXIT_FAILURE);
  483. - }
  484. -
  485. - datasz = shdr.sh_offset;
  486. - }
  487. - else if (e32_hdr.e_ident[EI_CLASS] == ELFCLASS64) {
  488. - const char *shstrtab, *name;
  489. - Elf64_Shdr shdr;
  490. + datasz = phdr.p_filesz;
  491. + memsz = phdr.p_memsz;
  492. + } else if (e32_hdr.e_ident[EI_CLASS] == ELFCLASS64) {
  493. + Elf64_Phdr phdr;
  494. int i;
  495. - void *strtab;
  496. -
  497. - fseek(f_in, shoff, SEEK_SET);
  498. - /* First find the strtab section */
  499. - fseek(f_in, shstrndx * shentsize, SEEK_CUR);
  500. - fread(&shdr, sizeof(shdr), 1, f_in);
  501. -
  502. - strtab = malloc(shdr.sh_size);
  503. - if (!strtab) {
  504. - fprintf(stderr, "Failed to malloc strtab\n");
  505. - exit(EXIT_FAILURE);
  506. - }
  507. + /* Find the first LOAD program header */
  508. + for (i = 0; i < phnum; i++) {
  509. + fseek(f_in, phoff + i * phentsize, SEEK_SET);
  510. + fread(&phdr, sizeof(phdr), 1, f_in);
  511. - fseek(f_in, shdr.sh_offset, SEEK_SET);
  512. - fread(strtab, shdr.sh_size, 1, f_in);
  513. -
  514. - /* Now search for the .bss section */
  515. - fseek(f_in, shoff, SEEK_SET);
  516. - for (i = 0; i < shnum; i++) {
  517. - rv = fread(&shdr, sizeof(shdr), 1, f_in);
  518. - if (!rv) {
  519. - fprintf(stderr, "Failed to read section table\n");
  520. - exit(EXIT_FAILURE);
  521. - }
  522. -
  523. - name = strtab + shdr.sh_name;
  524. - if (!strcmp(name, ".bss"))
  525. + if (phdr.p_type == PT_LOAD)
  526. break;
  527. }
  528. - if (i == shnum) {
  529. - fprintf(stderr, "Failed to find .bss section\n");
  530. - exit(EXIT_FAILURE);
  531. - }
  532. -
  533. - datasz = shdr.sh_offset;
  534. + datasz = phdr.p_filesz;
  535. + memsz = phdr.p_memsz;
  536. }
  537. buf = malloc(datasz);
  538. @@ -330,7 +271,7 @@ int main(int argc, char **argv)
  539. exit(EXIT_FAILURE);
  540. }
  541. - write_header(f_out, entry, datasz, st.st_size, class);
  542. + write_header(f_out, entry, datasz, memsz, class);
  543. /* Write out the entire ELF shared object */
  544. rewind(f_in);
  545. @@ -341,5 +282,8 @@ int main(int argc, char **argv)
  546. }
  547. fwrite(buf, datasz, rv, f_out);
  548. + free(buf);
  549. + fclose(f_out);
  550. + fclose(f_in);
  551. return 0;
  552. }
  553. diff --git a/efi/x86_64/syslinux.ld b/efi/x86_64/syslinux.ld
  554. index 95160bd..450641c 100644
  555. --- a/efi/x86_64/syslinux.ld
  556. +++ b/efi/x86_64/syslinux.ld
  557. @@ -136,7 +136,7 @@ SECTIONS
  558. *(.strtab)
  559. }
  560. - .bss : {
  561. + .bss (NOLOAD) : {
  562. /* the EFI loader doesn't seem to like a .bss section,
  563. so we stick it all into .data: */
  564. __bss_start = .;
  565. @@ -153,7 +153,7 @@ SECTIONS
  566. __bss_dwords = (__bss_len + 3) >> 2;
  567. . = ALIGN(128);
  568. -
  569. +
  570. /* Very large objects which don't need to be zeroed */
  571. .hugebss : {