grub-mkimagexx.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449
  1. /* grub-mkimage.c - make a bootable image */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <config.h>
  20. #include <grub/types.h>
  21. #include <grub/elf.h>
  22. #include <grub/aout.h>
  23. #include <grub/i18n.h>
  24. #include <grub/kernel.h>
  25. #include <grub/disk.h>
  26. #include <grub/emu/misc.h>
  27. #include <grub/util/misc.h>
  28. #include <grub/util/resolve.h>
  29. #include <grub/misc.h>
  30. #include <grub/offsets.h>
  31. #include <grub/crypto.h>
  32. #include <grub/dl.h>
  33. #include <time.h>
  34. #include <multiboot.h>
  35. #include <stdio.h>
  36. #include <unistd.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #include <assert.h>
  40. #include <grub/efi/pe32.h>
  41. #include <grub/uboot/image.h>
  42. #include <grub/arm/reloc.h>
  43. #include <grub/arm64/reloc.h>
  44. #include <grub/ia64/reloc.h>
  45. #include <grub/osdep/hostfile.h>
  46. #include <grub/util/install.h>
  47. #include <grub/util/mkimage.h>
  48. #include <xen/elfnote.h>
  49. #pragma GCC diagnostic ignored "-Wcast-align"
  50. #define GRUB_MKIMAGEXX
  51. #if !defined(MKIMAGE_ELF32) && !defined(MKIMAGE_ELF64)
  52. #if __SIZEOF_POINTER__ == 8
  53. #include "grub-mkimage64.c"
  54. #else
  55. #include "grub-mkimage32.c"
  56. #endif
  57. #endif
  58. /* These structures are defined according to the CHRP binding to IEEE1275,
  59. "Client Program Format" section. */
  60. struct grub_ieee1275_note_desc
  61. {
  62. grub_uint32_t real_mode;
  63. grub_uint32_t real_base;
  64. grub_uint32_t real_size;
  65. grub_uint32_t virt_base;
  66. grub_uint32_t virt_size;
  67. grub_uint32_t load_base;
  68. };
  69. #define GRUB_IEEE1275_NOTE_NAME "PowerPC"
  70. #define GRUB_IEEE1275_NOTE_TYPE 0x1275
  71. struct grub_ieee1275_note
  72. {
  73. Elf32_Nhdr header;
  74. char name[ALIGN_UP(sizeof (GRUB_IEEE1275_NOTE_NAME), 4)];
  75. struct grub_ieee1275_note_desc descriptor;
  76. };
  77. #define GRUB_XEN_NOTE_NAME "Xen"
  78. struct fixup_block_list
  79. {
  80. struct fixup_block_list *next;
  81. int state;
  82. struct grub_pe32_fixup_block b;
  83. };
  84. #define ALIGN_ADDR(x) (ALIGN_UP((x), image_target->voidp_sizeof))
  85. struct section_metadata
  86. {
  87. Elf_Half num_sections;
  88. Elf_Shdr *sections;
  89. Elf_Addr *addrs;
  90. Elf_Addr *vaddrs;
  91. Elf_Half section_entsize;
  92. Elf_Shdr *symtab;
  93. const char *strtab;
  94. };
  95. static int
  96. is_relocatable (const struct grub_install_image_target_desc *image_target)
  97. {
  98. return image_target->id == IMAGE_EFI || image_target->id == IMAGE_UBOOT
  99. || (image_target->id == IMAGE_COREBOOT && image_target->elf_target == EM_ARM);
  100. }
  101. #ifdef MKIMAGE_ELF32
  102. /*
  103. * R_ARM_THM_CALL/THM_JUMP24
  104. *
  105. * Relocate Thumb (T32) instruction set relative branches:
  106. * B.W, BL and BLX
  107. */
  108. static grub_err_t
  109. grub_arm_reloc_thm_call (grub_uint16_t *target, Elf32_Addr sym_addr)
  110. {
  111. grub_int32_t offset;
  112. offset = grub_arm_thm_call_get_offset (target);
  113. grub_dprintf ("dl", " sym_addr = 0x%08x", sym_addr);
  114. offset += sym_addr;
  115. grub_dprintf("dl", " BL*: target=%p, sym_addr=0x%08x, offset=%d\n",
  116. target, sym_addr, offset);
  117. /* Keep traditional (pre-Thumb2) limits on blx. In any case if the kernel
  118. is bigger than 2M (currently under 150K) then we probably have a problem
  119. somewhere else. */
  120. if (offset < -0x200000 || offset >= 0x200000)
  121. return grub_error (GRUB_ERR_BAD_MODULE,
  122. "THM_CALL Relocation out of range.");
  123. grub_dprintf ("dl", " relative destination = %p",
  124. (char *) target + offset);
  125. return grub_arm_thm_call_set_offset (target, offset);
  126. }
  127. /*
  128. * R_ARM_THM_JUMP19
  129. *
  130. * Relocate conditional Thumb (T32) B<c>.W
  131. */
  132. static grub_err_t
  133. grub_arm_reloc_thm_jump19 (grub_uint16_t *target, Elf32_Addr sym_addr)
  134. {
  135. grub_int32_t offset;
  136. if (!(sym_addr & 1))
  137. return grub_error (GRUB_ERR_BAD_MODULE,
  138. "Relocation targeting wrong execution state");
  139. offset = grub_arm_thm_jump19_get_offset (target);
  140. /* Adjust and re-truncate offset */
  141. offset += sym_addr;
  142. if (!grub_arm_thm_jump19_check_offset (offset))
  143. return grub_error (GRUB_ERR_BAD_MODULE,
  144. "THM_JUMP19 Relocation out of range.");
  145. grub_arm_thm_jump19_set_offset (target, offset);
  146. return GRUB_ERR_NONE;
  147. }
  148. /*
  149. * R_ARM_JUMP24
  150. *
  151. * Relocate ARM (A32) B
  152. */
  153. static grub_err_t
  154. grub_arm_reloc_jump24 (grub_uint32_t *target, Elf32_Addr sym_addr)
  155. {
  156. grub_int32_t offset;
  157. if (sym_addr & 1)
  158. return grub_error (GRUB_ERR_BAD_MODULE,
  159. "Relocation targeting wrong execution state");
  160. offset = grub_arm_jump24_get_offset (target);
  161. offset += sym_addr;
  162. if (!grub_arm_jump24_check_offset (offset))
  163. return grub_error (GRUB_ERR_BAD_MODULE,
  164. "JUMP24 Relocation out of range.");
  165. grub_arm_jump24_set_offset (target, offset);
  166. return GRUB_ERR_NONE;
  167. }
  168. #endif
  169. void
  170. SUFFIX (grub_mkimage_generate_elf) (const struct grub_install_image_target_desc *image_target,
  171. int note, char **core_img, size_t *core_size,
  172. Elf_Addr target_addr,
  173. struct grub_mkimage_layout *layout)
  174. {
  175. char *elf_img;
  176. size_t program_size;
  177. Elf_Ehdr *ehdr;
  178. Elf_Phdr *phdr;
  179. Elf_Shdr *shdr;
  180. int header_size, footer_size = 0;
  181. int phnum = 1;
  182. int shnum = 4;
  183. int string_size = sizeof (".text") + sizeof ("mods") + 1;
  184. if (image_target->id != IMAGE_LOONGSON_ELF)
  185. phnum += 2;
  186. if (note)
  187. {
  188. phnum++;
  189. footer_size += sizeof (struct grub_ieee1275_note);
  190. }
  191. if (image_target->id == IMAGE_XEN || image_target->id == IMAGE_XEN_PVH)
  192. {
  193. phnum++;
  194. shnum++;
  195. string_size += sizeof (".xen");
  196. footer_size += (image_target->id == IMAGE_XEN) ? XEN_NOTE_SIZE : XEN_PVH_NOTE_SIZE;
  197. }
  198. header_size = ALIGN_UP (sizeof (*ehdr) + phnum * sizeof (*phdr)
  199. + shnum * sizeof (*shdr) + string_size, layout->align);
  200. program_size = ALIGN_ADDR (*core_size);
  201. elf_img = xmalloc (program_size + header_size + footer_size);
  202. memset (elf_img, 0, program_size + header_size + footer_size);
  203. memcpy (elf_img + header_size, *core_img, *core_size);
  204. ehdr = (void *) elf_img;
  205. phdr = (void *) (elf_img + sizeof (*ehdr));
  206. shdr = (void *) (elf_img + sizeof (*ehdr) + phnum * sizeof (*phdr));
  207. memcpy (ehdr->e_ident, ELFMAG, SELFMAG);
  208. ehdr->e_ident[EI_CLASS] = ELFCLASSXX;
  209. if (!image_target->bigendian)
  210. ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
  211. else
  212. ehdr->e_ident[EI_DATA] = ELFDATA2MSB;
  213. ehdr->e_ident[EI_VERSION] = EV_CURRENT;
  214. ehdr->e_ident[EI_OSABI] = ELFOSABI_NONE;
  215. ehdr->e_type = grub_host_to_target16 (ET_EXEC);
  216. ehdr->e_machine = grub_host_to_target16 (image_target->elf_target);
  217. ehdr->e_version = grub_host_to_target32 (EV_CURRENT);
  218. ehdr->e_phoff = grub_host_to_target32 ((char *) phdr - (char *) ehdr);
  219. ehdr->e_phentsize = grub_host_to_target16 (sizeof (*phdr));
  220. ehdr->e_phnum = grub_host_to_target16 (phnum);
  221. ehdr->e_shoff = grub_host_to_target32 ((grub_uint8_t *) shdr
  222. - (grub_uint8_t *) ehdr);
  223. if (image_target->id == IMAGE_LOONGSON_ELF)
  224. ehdr->e_shentsize = grub_host_to_target16 (0);
  225. else
  226. ehdr->e_shentsize = grub_host_to_target16 (sizeof (Elf_Shdr));
  227. ehdr->e_shnum = grub_host_to_target16 (shnum);
  228. ehdr->e_shstrndx = grub_host_to_target16 (1);
  229. ehdr->e_ehsize = grub_host_to_target16 (sizeof (*ehdr));
  230. phdr->p_type = grub_host_to_target32 (PT_LOAD);
  231. phdr->p_offset = grub_host_to_target32 (header_size);
  232. phdr->p_flags = grub_host_to_target32 (PF_R | PF_W | PF_X);
  233. ehdr->e_entry = grub_host_to_target32 (target_addr);
  234. phdr->p_vaddr = grub_host_to_target32 (target_addr);
  235. phdr->p_paddr = grub_host_to_target32 (target_addr);
  236. phdr->p_align = grub_host_to_target32 (layout->align > image_target->link_align ?
  237. layout->align : image_target->link_align);
  238. if (image_target->id == IMAGE_LOONGSON_ELF)
  239. ehdr->e_flags = grub_host_to_target32 (0x1000 | EF_MIPS_NOREORDER
  240. | EF_MIPS_PIC | EF_MIPS_CPIC);
  241. else
  242. ehdr->e_flags = 0;
  243. if (image_target->id == IMAGE_LOONGSON_ELF)
  244. {
  245. phdr->p_filesz = grub_host_to_target32 (*core_size);
  246. phdr->p_memsz = grub_host_to_target32 (*core_size);
  247. }
  248. else
  249. {
  250. grub_uint32_t target_addr_mods;
  251. phdr->p_filesz = grub_host_to_target32 (layout->kernel_size);
  252. if (image_target->id == IMAGE_COREBOOT && image_target->elf_target == EM_ARM)
  253. phdr->p_memsz = grub_host_to_target32 (layout->kernel_size);
  254. else
  255. phdr->p_memsz = grub_host_to_target32 (layout->kernel_size + layout->bss_size);
  256. phdr++;
  257. phdr->p_type = grub_host_to_target32 (PT_GNU_STACK);
  258. phdr->p_offset = grub_host_to_target32 (header_size + layout->kernel_size);
  259. phdr->p_paddr = phdr->p_vaddr = phdr->p_filesz = phdr->p_memsz = 0;
  260. phdr->p_flags = grub_host_to_target32 (PF_R | PF_W | PF_X);
  261. phdr->p_align = grub_host_to_target32 (image_target->link_align);
  262. phdr++;
  263. phdr->p_type = grub_host_to_target32 (PT_LOAD);
  264. phdr->p_offset = grub_host_to_target32 (header_size + layout->kernel_size);
  265. phdr->p_flags = grub_host_to_target32 (PF_R | PF_W | PF_X);
  266. phdr->p_filesz = phdr->p_memsz
  267. = grub_host_to_target32 (*core_size - layout->kernel_size);
  268. if (image_target->id == IMAGE_COREBOOT && image_target->elf_target == EM_386)
  269. target_addr_mods = GRUB_KERNEL_I386_COREBOOT_MODULES_ADDR;
  270. else if (image_target->id == IMAGE_COREBOOT && image_target->elf_target == EM_ARM)
  271. target_addr_mods = ALIGN_UP (target_addr + layout->end
  272. + image_target->mod_gap,
  273. image_target->mod_align);
  274. else
  275. target_addr_mods = ALIGN_UP (target_addr + layout->kernel_size + layout->bss_size
  276. + image_target->mod_gap,
  277. image_target->mod_align);
  278. phdr->p_vaddr = grub_host_to_target_addr (target_addr_mods);
  279. phdr->p_paddr = grub_host_to_target_addr (target_addr_mods);
  280. phdr->p_align = grub_host_to_target32 (image_target->link_align);
  281. }
  282. if (image_target->id == IMAGE_XEN)
  283. {
  284. char *note_start = (elf_img + program_size + header_size);
  285. Elf_Nhdr *note_ptr;
  286. char *ptr = (char *) note_start;
  287. grub_util_info ("adding XEN NOTE segment");
  288. /* Guest OS. */
  289. note_ptr = (Elf_Nhdr *) ptr;
  290. note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
  291. note_ptr->n_descsz = grub_host_to_target32 (sizeof (PACKAGE_NAME));
  292. note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_GUEST_OS);
  293. ptr += sizeof (Elf_Nhdr);
  294. memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
  295. ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
  296. memcpy (ptr, PACKAGE_NAME, sizeof (PACKAGE_NAME));
  297. ptr += ALIGN_UP (sizeof (PACKAGE_NAME), 4);
  298. /* Loader. */
  299. note_ptr = (Elf_Nhdr *) ptr;
  300. note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
  301. note_ptr->n_descsz = grub_host_to_target32 (sizeof ("generic"));
  302. note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_LOADER);
  303. ptr += sizeof (Elf_Nhdr);
  304. memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
  305. ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
  306. memcpy (ptr, "generic", sizeof ("generic"));
  307. ptr += ALIGN_UP (sizeof ("generic"), 4);
  308. /* Version. */
  309. note_ptr = (Elf_Nhdr *) ptr;
  310. note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
  311. note_ptr->n_descsz = grub_host_to_target32 (sizeof ("xen-3.0"));
  312. note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_XEN_VERSION);
  313. ptr += sizeof (Elf_Nhdr);
  314. memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
  315. ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
  316. memcpy (ptr, "xen-3.0", sizeof ("xen-3.0"));
  317. ptr += ALIGN_UP (sizeof ("xen-3.0"), 4);
  318. /* Entry. */
  319. note_ptr = (Elf_Nhdr *) ptr;
  320. note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
  321. note_ptr->n_descsz = grub_host_to_target32 (image_target->voidp_sizeof);
  322. note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_ENTRY);
  323. ptr += sizeof (Elf_Nhdr);
  324. memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
  325. ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
  326. memset (ptr, 0, image_target->voidp_sizeof);
  327. ptr += image_target->voidp_sizeof;
  328. /* Virt base. */
  329. note_ptr = (Elf_Nhdr *) ptr;
  330. note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
  331. note_ptr->n_descsz = grub_host_to_target32 (image_target->voidp_sizeof);
  332. note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_VIRT_BASE);
  333. ptr += sizeof (Elf_Nhdr);
  334. memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
  335. ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
  336. memset (ptr, 0, image_target->voidp_sizeof);
  337. ptr += image_target->voidp_sizeof;
  338. /* PAE. */
  339. if (image_target->elf_target == EM_386)
  340. {
  341. note_ptr = (Elf_Nhdr *) ptr;
  342. note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
  343. note_ptr->n_descsz = grub_host_to_target32 (sizeof ("yes,bimodal"));
  344. note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_PAE_MODE);
  345. ptr += sizeof (Elf_Nhdr);
  346. memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
  347. ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
  348. memcpy (ptr, "yes", sizeof ("yes"));
  349. ptr += ALIGN_UP (sizeof ("yes"), 4);
  350. }
  351. assert (XEN_NOTE_SIZE == (ptr - note_start));
  352. phdr++;
  353. phdr->p_type = grub_host_to_target32 (PT_NOTE);
  354. phdr->p_flags = grub_host_to_target32 (PF_R);
  355. phdr->p_align = grub_host_to_target32 (image_target->voidp_sizeof);
  356. phdr->p_vaddr = 0;
  357. phdr->p_paddr = 0;
  358. phdr->p_filesz = grub_host_to_target32 (XEN_NOTE_SIZE);
  359. phdr->p_memsz = 0;
  360. phdr->p_offset = grub_host_to_target32 (header_size + program_size);
  361. }
  362. if (image_target->id == IMAGE_XEN_PVH)
  363. {
  364. char *note_start = (elf_img + program_size + header_size);
  365. Elf_Nhdr *note_ptr;
  366. char *ptr = (char *) note_start;
  367. grub_util_info ("adding XEN NOTE segment");
  368. /* Phys32 Entry. */
  369. note_ptr = (Elf_Nhdr *) ptr;
  370. note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
  371. note_ptr->n_descsz = grub_host_to_target32 (image_target->voidp_sizeof);
  372. note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_PHYS32_ENTRY);
  373. ptr += sizeof (Elf_Nhdr);
  374. memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
  375. ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
  376. memset (ptr, 0, image_target->voidp_sizeof);
  377. *(grub_uint32_t *) ptr = GRUB_KERNEL_I386_XEN_PVH_LINK_ADDR;
  378. ptr += image_target->voidp_sizeof;
  379. assert (XEN_PVH_NOTE_SIZE == (ptr - note_start));
  380. phdr++;
  381. phdr->p_type = grub_host_to_target32 (PT_NOTE);
  382. phdr->p_flags = grub_host_to_target32 (PF_R);
  383. phdr->p_align = grub_host_to_target32 (image_target->voidp_sizeof);
  384. phdr->p_vaddr = 0;
  385. phdr->p_paddr = 0;
  386. phdr->p_filesz = grub_host_to_target32 (XEN_PVH_NOTE_SIZE);
  387. phdr->p_memsz = 0;
  388. phdr->p_offset = grub_host_to_target32 (header_size + program_size);
  389. }
  390. if (note)
  391. {
  392. int note_size = sizeof (struct grub_ieee1275_note);
  393. struct grub_ieee1275_note *note_ptr = (struct grub_ieee1275_note *)
  394. (elf_img + program_size + header_size);
  395. grub_util_info ("adding CHRP NOTE segment");
  396. note_ptr->header.n_namesz = grub_host_to_target32 (sizeof (GRUB_IEEE1275_NOTE_NAME));
  397. note_ptr->header.n_descsz = grub_host_to_target32 (sizeof (struct grub_ieee1275_note_desc));
  398. note_ptr->header.n_type = grub_host_to_target32 (GRUB_IEEE1275_NOTE_TYPE);
  399. strcpy (note_ptr->name, GRUB_IEEE1275_NOTE_NAME);
  400. note_ptr->descriptor.real_mode = grub_host_to_target32 (0xffffffff);
  401. note_ptr->descriptor.real_base = grub_host_to_target32 (0x00c00000);
  402. note_ptr->descriptor.real_size = grub_host_to_target32 (0xffffffff);
  403. note_ptr->descriptor.virt_base = grub_host_to_target32 (0xffffffff);
  404. note_ptr->descriptor.virt_size = grub_host_to_target32 (0xffffffff);
  405. note_ptr->descriptor.load_base = grub_host_to_target32 (0x00004000);
  406. phdr++;
  407. phdr->p_type = grub_host_to_target32 (PT_NOTE);
  408. phdr->p_flags = grub_host_to_target32 (PF_R);
  409. phdr->p_align = grub_host_to_target32 (image_target->voidp_sizeof);
  410. phdr->p_vaddr = 0;
  411. phdr->p_paddr = 0;
  412. phdr->p_filesz = grub_host_to_target32 (note_size);
  413. phdr->p_memsz = 0;
  414. phdr->p_offset = grub_host_to_target32 (header_size + program_size);
  415. }
  416. {
  417. char *str_start = (elf_img + sizeof (*ehdr) + phnum * sizeof (*phdr)
  418. + shnum * sizeof (*shdr));
  419. char *ptr = str_start + 1;
  420. shdr++;
  421. shdr->sh_name = grub_host_to_target32 (0);
  422. shdr->sh_type = grub_host_to_target32 (SHT_STRTAB);
  423. shdr->sh_addr = grub_host_to_target_addr (0);
  424. shdr->sh_offset = grub_host_to_target_addr (str_start - elf_img);
  425. shdr->sh_size = grub_host_to_target32 (string_size);
  426. shdr->sh_link = grub_host_to_target32 (0);
  427. shdr->sh_info = grub_host_to_target32 (0);
  428. shdr->sh_addralign = grub_host_to_target32 (layout->align);
  429. shdr->sh_entsize = grub_host_to_target32 (0);
  430. shdr++;
  431. memcpy (ptr, ".text", sizeof (".text"));
  432. shdr->sh_name = grub_host_to_target32 (ptr - str_start);
  433. ptr += sizeof (".text");
  434. shdr->sh_type = grub_host_to_target32 (SHT_PROGBITS);
  435. shdr->sh_addr = grub_host_to_target_addr (target_addr);
  436. shdr->sh_offset = grub_host_to_target_addr (header_size);
  437. shdr->sh_size = grub_host_to_target32 (layout->kernel_size);
  438. shdr->sh_link = grub_host_to_target32 (0);
  439. shdr->sh_info = grub_host_to_target32 (0);
  440. shdr->sh_addralign = grub_host_to_target32 (layout->align);
  441. shdr->sh_entsize = grub_host_to_target32 (0);
  442. shdr++;
  443. memcpy (ptr, "mods", sizeof ("mods"));
  444. shdr->sh_name = grub_host_to_target32 (ptr - str_start);
  445. ptr += sizeof ("mods");
  446. shdr->sh_type = grub_host_to_target32 (SHT_PROGBITS);
  447. shdr->sh_addr = grub_host_to_target_addr (target_addr + layout->kernel_size);
  448. shdr->sh_offset = grub_host_to_target_addr (header_size + layout->kernel_size);
  449. shdr->sh_size = grub_host_to_target32 (*core_size - layout->kernel_size);
  450. shdr->sh_link = grub_host_to_target32 (0);
  451. shdr->sh_info = grub_host_to_target32 (0);
  452. shdr->sh_addralign = grub_host_to_target32 (image_target->voidp_sizeof);
  453. shdr->sh_entsize = grub_host_to_target32 (0);
  454. shdr++;
  455. if (image_target->id == IMAGE_XEN || image_target->id == IMAGE_XEN_PVH)
  456. {
  457. memcpy (ptr, ".xen", sizeof (".xen"));
  458. shdr->sh_name = grub_host_to_target32 (ptr - str_start);
  459. ptr += sizeof (".xen");
  460. shdr->sh_type = grub_host_to_target32 (SHT_PROGBITS);
  461. shdr->sh_addr = grub_host_to_target_addr (target_addr + layout->kernel_size);
  462. shdr->sh_offset = grub_host_to_target_addr (program_size + header_size);
  463. if (image_target->id == IMAGE_XEN)
  464. shdr->sh_size = grub_host_to_target32 (XEN_NOTE_SIZE);
  465. else
  466. shdr->sh_size = grub_host_to_target32 (XEN_PVH_NOTE_SIZE);
  467. shdr->sh_link = grub_host_to_target32 (0);
  468. shdr->sh_info = grub_host_to_target32 (0);
  469. shdr->sh_addralign = grub_host_to_target32 (image_target->voidp_sizeof);
  470. shdr->sh_entsize = grub_host_to_target32 (0);
  471. shdr++;
  472. }
  473. }
  474. free (*core_img);
  475. *core_img = elf_img;
  476. *core_size = program_size + header_size + footer_size;
  477. }
  478. /* Relocate symbols; note that this function overwrites the symbol table.
  479. Return the address of a start symbol. */
  480. static Elf_Addr
  481. SUFFIX (relocate_symbols) (Elf_Ehdr *e, struct section_metadata *smd,
  482. void *jumpers, Elf_Addr jumpers_addr,
  483. Elf_Addr bss_start, Elf_Addr end,
  484. const struct grub_install_image_target_desc *image_target)
  485. {
  486. Elf_Word symtab_size, sym_size, num_syms;
  487. Elf_Off symtab_offset;
  488. Elf_Addr start_address = (Elf_Addr) -1;
  489. Elf_Sym *sym;
  490. Elf_Word i;
  491. Elf_Shdr *symtab_section;
  492. const char *symtab;
  493. grub_uint64_t *jptr = jumpers;
  494. symtab_section = (Elf_Shdr *) ((char *) smd->sections
  495. + grub_target_to_host32 (smd->symtab->sh_link)
  496. * smd->section_entsize);
  497. symtab = (char *) e + grub_target_to_host (symtab_section->sh_offset);
  498. symtab_size = grub_target_to_host (smd->symtab->sh_size);
  499. sym_size = grub_target_to_host (smd->symtab->sh_entsize);
  500. symtab_offset = grub_target_to_host (smd->symtab->sh_offset);
  501. num_syms = symtab_size / sym_size;
  502. for (i = 0, sym = (Elf_Sym *) ((char *) e + symtab_offset);
  503. i < num_syms;
  504. i++, sym = (Elf_Sym *) ((char *) sym + sym_size))
  505. {
  506. Elf_Section cur_index;
  507. const char *name;
  508. name = symtab + grub_target_to_host32 (sym->st_name);
  509. cur_index = grub_target_to_host16 (sym->st_shndx);
  510. if (cur_index == STN_ABS)
  511. {
  512. continue;
  513. }
  514. else if (cur_index == STN_UNDEF)
  515. {
  516. if (sym->st_name && grub_strcmp (name, "__bss_start") == 0)
  517. sym->st_value = bss_start;
  518. else if (sym->st_name && grub_strcmp (name, "_end") == 0)
  519. sym->st_value = end;
  520. else if (sym->st_name)
  521. grub_util_error ("undefined symbol %s", name);
  522. else
  523. continue;
  524. }
  525. else if (cur_index >= smd->num_sections)
  526. grub_util_error ("section %d does not exist", cur_index);
  527. else
  528. {
  529. sym->st_value = (grub_target_to_host (sym->st_value)
  530. + smd->vaddrs[cur_index]);
  531. }
  532. if (image_target->elf_target == EM_IA_64 && ELF_ST_TYPE (sym->st_info)
  533. == STT_FUNC)
  534. {
  535. *jptr = grub_host_to_target64 (sym->st_value);
  536. sym->st_value = (char *) jptr - (char *) jumpers + jumpers_addr;
  537. jptr++;
  538. *jptr = 0;
  539. jptr++;
  540. }
  541. grub_util_info ("locating %s at 0x%" GRUB_HOST_PRIxLONG_LONG
  542. " (0x%" GRUB_HOST_PRIxLONG_LONG ")", name,
  543. (unsigned long long) sym->st_value,
  544. (unsigned long long) smd->vaddrs[cur_index]);
  545. if (start_address == (Elf_Addr)-1)
  546. if (strcmp (name, "_start") == 0 || strcmp (name, "start") == 0)
  547. start_address = sym->st_value;
  548. }
  549. return start_address;
  550. }
  551. /* Return the address of a symbol at the index I in the section S. */
  552. static Elf_Addr
  553. SUFFIX (get_symbol_address) (Elf_Ehdr *e, Elf_Shdr *s, Elf_Word i,
  554. const struct grub_install_image_target_desc *image_target)
  555. {
  556. Elf_Sym *sym;
  557. sym = (Elf_Sym *) ((char *) e
  558. + grub_target_to_host (s->sh_offset)
  559. + i * grub_target_to_host (s->sh_entsize));
  560. return sym->st_value;
  561. }
  562. /* Return the address of a modified value. */
  563. static Elf_Addr *
  564. SUFFIX (get_target_address) (Elf_Ehdr *e, Elf_Shdr *s, Elf_Addr offset,
  565. const struct grub_install_image_target_desc *image_target)
  566. {
  567. return (Elf_Addr *) ((char *) e + grub_target_to_host (s->sh_offset) + offset);
  568. }
  569. #ifdef MKIMAGE_ELF64
  570. static Elf_Addr
  571. SUFFIX (count_funcs) (Elf_Ehdr *e, Elf_Shdr *symtab_section,
  572. const struct grub_install_image_target_desc *image_target)
  573. {
  574. Elf_Word symtab_size, sym_size, num_syms;
  575. Elf_Off symtab_offset;
  576. Elf_Sym *sym;
  577. Elf_Word i;
  578. int ret = 0;
  579. symtab_size = grub_target_to_host (symtab_section->sh_size);
  580. sym_size = grub_target_to_host (symtab_section->sh_entsize);
  581. symtab_offset = grub_target_to_host (symtab_section->sh_offset);
  582. num_syms = symtab_size / sym_size;
  583. for (i = 0, sym = (Elf_Sym *) ((char *) e + symtab_offset);
  584. i < num_syms;
  585. i++, sym = (Elf_Sym *) ((char *) sym + sym_size))
  586. if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
  587. ret++;
  588. return ret;
  589. }
  590. #endif
  591. #ifdef MKIMAGE_ELF32
  592. /* Deal with relocation information. This function relocates addresses
  593. within the virtual address space starting from 0. So only relative
  594. addresses can be fully resolved. Absolute addresses must be relocated
  595. again by a PE32 relocator when loaded. */
  596. static grub_size_t
  597. arm_get_trampoline_size (Elf_Ehdr *e,
  598. Elf_Shdr *sections,
  599. Elf_Half section_entsize,
  600. Elf_Half num_sections,
  601. const struct grub_install_image_target_desc *image_target)
  602. {
  603. Elf_Half i;
  604. Elf_Shdr *s;
  605. grub_size_t ret = 0;
  606. for (i = 0, s = sections;
  607. i < num_sections;
  608. i++, s = (Elf_Shdr *) ((char *) s + section_entsize))
  609. if ((s->sh_type == grub_host_to_target32 (SHT_REL)) ||
  610. (s->sh_type == grub_host_to_target32 (SHT_RELA)))
  611. {
  612. Elf_Rela *r;
  613. Elf_Word rtab_size, r_size, num_rs;
  614. Elf_Off rtab_offset;
  615. Elf_Shdr *symtab_section;
  616. Elf_Word j;
  617. symtab_section = (Elf_Shdr *) ((char *) sections
  618. + (grub_target_to_host32 (s->sh_link)
  619. * section_entsize));
  620. rtab_size = grub_target_to_host (s->sh_size);
  621. r_size = grub_target_to_host (s->sh_entsize);
  622. rtab_offset = grub_target_to_host (s->sh_offset);
  623. num_rs = rtab_size / r_size;
  624. for (j = 0, r = (Elf_Rela *) ((char *) e + rtab_offset);
  625. j < num_rs;
  626. j++, r = (Elf_Rela *) ((char *) r + r_size))
  627. {
  628. Elf_Addr info;
  629. Elf_Addr sym_addr;
  630. info = grub_target_to_host (r->r_info);
  631. sym_addr = SUFFIX (get_symbol_address) (e, symtab_section,
  632. ELF_R_SYM (info), image_target);
  633. sym_addr += (s->sh_type == grub_target_to_host32 (SHT_RELA)) ?
  634. grub_target_to_host (r->r_addend) : 0;
  635. switch (ELF_R_TYPE (info))
  636. {
  637. case R_ARM_ABS32:
  638. case R_ARM_V4BX:
  639. break;
  640. case R_ARM_THM_CALL:
  641. case R_ARM_THM_JUMP24:
  642. case R_ARM_THM_JUMP19:
  643. if (!(sym_addr & 1))
  644. ret += 8;
  645. break;
  646. case R_ARM_CALL:
  647. case R_ARM_JUMP24:
  648. if (sym_addr & 1)
  649. ret += 16;
  650. break;
  651. default:
  652. grub_util_error (_("relocation 0x%x is not implemented yet"),
  653. (unsigned int) ELF_R_TYPE (info));
  654. break;
  655. }
  656. }
  657. }
  658. return ret;
  659. }
  660. #endif
  661. static int
  662. SUFFIX (is_kept_section) (Elf_Shdr *s, const struct grub_install_image_target_desc *image_target);
  663. static int
  664. SUFFIX (is_kept_reloc_section) (Elf_Shdr *s, const struct grub_install_image_target_desc *image_target,
  665. struct section_metadata *smd);
  666. /* Deal with relocation information. This function relocates addresses
  667. within the virtual address space starting from 0. So only relative
  668. addresses can be fully resolved. Absolute addresses must be relocated
  669. again by a PE32 relocator when loaded. */
  670. static void
  671. SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct section_metadata *smd,
  672. char *pe_target, Elf_Addr tramp_off, Elf_Addr got_off,
  673. const struct grub_install_image_target_desc *image_target)
  674. {
  675. Elf_Half i;
  676. Elf_Shdr *s;
  677. #ifdef MKIMAGE_ELF64
  678. struct grub_ia64_trampoline *tr = (void *) (pe_target + tramp_off);
  679. grub_uint64_t *gpptr = (void *) (pe_target + got_off);
  680. unsigned unmatched_adr_got_page = 0;
  681. #define MASK19 ((1 << 19) - 1)
  682. #else
  683. grub_uint32_t *tr = (void *) (pe_target + tramp_off);
  684. #endif
  685. for (i = 0, s = smd->sections;
  686. i < smd->num_sections;
  687. i++, s = (Elf_Shdr *) ((char *) s + smd->section_entsize))
  688. if ((s->sh_type == grub_host_to_target32 (SHT_REL)) ||
  689. (s->sh_type == grub_host_to_target32 (SHT_RELA)))
  690. {
  691. Elf_Rela *r;
  692. Elf_Word rtab_size, r_size, num_rs;
  693. Elf_Off rtab_offset;
  694. Elf_Word target_section_index;
  695. Elf_Addr target_section_addr;
  696. Elf_Shdr *target_section;
  697. Elf_Word j;
  698. if (!SUFFIX (is_kept_section) (s, image_target) &&
  699. !SUFFIX (is_kept_reloc_section) (s, image_target, smd))
  700. {
  701. grub_util_info ("not translating relocations for omitted section %s",
  702. smd->strtab + grub_le_to_cpu32 (s->sh_name));
  703. continue;
  704. }
  705. target_section_index = grub_target_to_host32 (s->sh_info);
  706. target_section_addr = smd->addrs[target_section_index];
  707. target_section = (Elf_Shdr *) ((char *) smd->sections
  708. + (target_section_index
  709. * smd->section_entsize));
  710. grub_util_info ("dealing with the relocation section %s for %s",
  711. smd->strtab + grub_target_to_host32 (s->sh_name),
  712. smd->strtab + grub_target_to_host32 (target_section->sh_name));
  713. rtab_size = grub_target_to_host (s->sh_size);
  714. r_size = grub_target_to_host (s->sh_entsize);
  715. rtab_offset = grub_target_to_host (s->sh_offset);
  716. num_rs = rtab_size / r_size;
  717. for (j = 0, r = (Elf_Rela *) ((char *) e + rtab_offset);
  718. j < num_rs;
  719. j++, r = (Elf_Rela *) ((char *) r + r_size))
  720. {
  721. Elf_Addr info;
  722. Elf_Addr offset;
  723. Elf_Addr sym_addr;
  724. Elf_Addr *target;
  725. Elf_Addr addend;
  726. offset = grub_target_to_host (r->r_offset);
  727. target = SUFFIX (get_target_address) (e, target_section,
  728. offset, image_target);
  729. info = grub_target_to_host (r->r_info);
  730. sym_addr = SUFFIX (get_symbol_address) (e, smd->symtab,
  731. ELF_R_SYM (info), image_target);
  732. addend = (s->sh_type == grub_target_to_host32 (SHT_RELA)) ?
  733. grub_target_to_host (r->r_addend) : 0;
  734. switch (image_target->elf_target)
  735. {
  736. case EM_386:
  737. switch (ELF_R_TYPE (info))
  738. {
  739. case R_386_NONE:
  740. break;
  741. case R_386_32:
  742. /* This is absolute. */
  743. *target = grub_host_to_target32 (grub_target_to_host32 (*target)
  744. + addend + sym_addr);
  745. grub_util_info ("relocating an R_386_32 entry to 0x%"
  746. GRUB_HOST_PRIxLONG_LONG " at the offset 0x%"
  747. GRUB_HOST_PRIxLONG_LONG,
  748. (unsigned long long) *target,
  749. (unsigned long long) offset);
  750. break;
  751. case R_386_PC32:
  752. /* This is relative. */
  753. *target = grub_host_to_target32 (grub_target_to_host32 (*target)
  754. + addend + sym_addr
  755. - target_section_addr - offset
  756. - image_target->vaddr_offset);
  757. grub_util_info ("relocating an R_386_PC32 entry to 0x%"
  758. GRUB_HOST_PRIxLONG_LONG " at the offset 0x%"
  759. GRUB_HOST_PRIxLONG_LONG,
  760. (unsigned long long) *target,
  761. (unsigned long long) offset);
  762. break;
  763. default:
  764. grub_util_error (_("relocation 0x%x is not implemented yet"),
  765. (unsigned int) ELF_R_TYPE (info));
  766. break;
  767. }
  768. break;
  769. #ifdef MKIMAGE_ELF64
  770. case EM_X86_64:
  771. switch (ELF_R_TYPE (info))
  772. {
  773. case R_X86_64_NONE:
  774. break;
  775. case R_X86_64_64:
  776. *target = grub_host_to_target64 (grub_target_to_host64 (*target)
  777. + addend + sym_addr);
  778. grub_util_info ("relocating an R_X86_64_64 entry to 0x%"
  779. GRUB_HOST_PRIxLONG_LONG " at the offset 0x%"
  780. GRUB_HOST_PRIxLONG_LONG,
  781. (unsigned long long) *target,
  782. (unsigned long long) offset);
  783. break;
  784. case R_X86_64_PC32:
  785. case R_X86_64_PLT32:
  786. {
  787. grub_uint32_t *t32 = (grub_uint32_t *) target;
  788. *t32 = grub_host_to_target64 (grub_target_to_host32 (*t32)
  789. + addend + sym_addr
  790. - target_section_addr - offset
  791. - image_target->vaddr_offset);
  792. grub_util_info ("relocating an R_X86_64_PC32 entry to 0x%x at the offset 0x%"
  793. GRUB_HOST_PRIxLONG_LONG,
  794. *t32, (unsigned long long) offset);
  795. break;
  796. }
  797. case R_X86_64_PC64:
  798. {
  799. *target = grub_host_to_target64 (grub_target_to_host64 (*target)
  800. + addend + sym_addr
  801. - target_section_addr - offset
  802. - image_target->vaddr_offset);
  803. grub_util_info ("relocating an R_X86_64_PC64 entry to 0x%"
  804. GRUB_HOST_PRIxLONG_LONG " at the offset 0x%"
  805. GRUB_HOST_PRIxLONG_LONG,
  806. (unsigned long long) *target,
  807. (unsigned long long) offset);
  808. break;
  809. }
  810. case R_X86_64_32:
  811. case R_X86_64_32S:
  812. {
  813. grub_uint32_t *t32 = (grub_uint32_t *) target;
  814. *t32 = grub_host_to_target64 (grub_target_to_host32 (*t32)
  815. + addend + sym_addr);
  816. grub_util_info ("relocating an R_X86_64_32(S) entry to 0x%x at the offset 0x%"
  817. GRUB_HOST_PRIxLONG_LONG,
  818. *t32, (unsigned long long) offset);
  819. break;
  820. }
  821. default:
  822. grub_util_error (_("relocation 0x%x is not implemented yet"),
  823. (unsigned int) ELF_R_TYPE (info));
  824. break;
  825. }
  826. break;
  827. case EM_IA_64:
  828. switch (ELF_R_TYPE (info))
  829. {
  830. case R_IA64_PCREL21B:
  831. {
  832. grub_uint64_t noff;
  833. grub_ia64_make_trampoline (tr, addend + sym_addr);
  834. noff = ((char *) tr - (char *) pe_target
  835. - target_section_addr - (offset & ~3)) >> 4;
  836. tr++;
  837. if (noff & ~MASK19)
  838. grub_util_error ("trampoline offset too big (%"
  839. GRUB_HOST_PRIxLONG_LONG ")",
  840. (unsigned long long) noff);
  841. grub_ia64_add_value_to_slot_20b ((grub_addr_t) target, noff);
  842. }
  843. break;
  844. case R_IA64_LTOFF22X:
  845. case R_IA64_LTOFF22:
  846. {
  847. Elf_Sym *sym;
  848. sym = (Elf_Sym *) ((char *) e
  849. + grub_target_to_host (smd->symtab->sh_offset)
  850. + ELF_R_SYM (info) * grub_target_to_host (smd->symtab->sh_entsize));
  851. if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
  852. sym_addr = grub_target_to_host64 (*(grub_uint64_t *) (pe_target
  853. + sym->st_value
  854. - image_target->vaddr_offset));
  855. }
  856. /* FALLTHROUGH */
  857. case R_IA64_LTOFF_FPTR22:
  858. *gpptr = grub_host_to_target64 (addend + sym_addr);
  859. grub_ia64_add_value_to_slot_21 ((grub_addr_t) target,
  860. (char *) gpptr - (char *) pe_target
  861. + image_target->vaddr_offset);
  862. gpptr++;
  863. break;
  864. case R_IA64_GPREL22:
  865. grub_ia64_add_value_to_slot_21 ((grub_addr_t) target,
  866. addend + sym_addr);
  867. break;
  868. case R_IA64_GPREL64I:
  869. grub_ia64_set_immu64 ((grub_addr_t) target,
  870. addend + sym_addr);
  871. break;
  872. case R_IA64_PCREL64LSB:
  873. *target = grub_host_to_target64 (grub_target_to_host64 (*target)
  874. + addend + sym_addr
  875. - target_section_addr - offset
  876. - image_target->vaddr_offset);
  877. break;
  878. case R_IA64_SEGREL64LSB:
  879. *target = grub_host_to_target64 (grub_target_to_host64 (*target)
  880. + addend + sym_addr - target_section_addr);
  881. break;
  882. case R_IA64_DIR64LSB:
  883. case R_IA64_FPTR64LSB:
  884. *target = grub_host_to_target64 (grub_target_to_host64 (*target)
  885. + addend + sym_addr);
  886. grub_util_info ("relocating a direct entry to 0x%"
  887. GRUB_HOST_PRIxLONG_LONG " at the offset 0x%"
  888. GRUB_HOST_PRIxLONG_LONG,
  889. (unsigned long long)
  890. grub_target_to_host64 (*target),
  891. (unsigned long long) offset);
  892. break;
  893. /* We treat LTOFF22X as LTOFF22, so we can ignore LDXMOV. */
  894. case R_IA64_LDXMOV:
  895. break;
  896. default:
  897. grub_util_error (_("relocation 0x%x is not implemented yet"),
  898. (unsigned int) ELF_R_TYPE (info));
  899. break;
  900. }
  901. break;
  902. case EM_AARCH64:
  903. {
  904. sym_addr += addend;
  905. switch (ELF_R_TYPE (info))
  906. {
  907. case R_AARCH64_ABS64:
  908. {
  909. *target = grub_host_to_target64 (grub_target_to_host64 (*target) + sym_addr);
  910. }
  911. break;
  912. case R_AARCH64_PREL32:
  913. {
  914. grub_uint32_t *t32 = (grub_uint32_t *) target;
  915. *t32 = grub_host_to_target64 (grub_target_to_host32 (*t32)
  916. + sym_addr
  917. - target_section_addr - offset
  918. - image_target->vaddr_offset);
  919. grub_util_info ("relocating an R_AARCH64_PREL32 entry to 0x%x at the offset 0x%"
  920. GRUB_HOST_PRIxLONG_LONG,
  921. *t32, (unsigned long long) offset);
  922. break;
  923. }
  924. case R_AARCH64_ADD_ABS_LO12_NC:
  925. grub_arm64_set_abs_lo12 ((grub_uint32_t *) target,
  926. sym_addr);
  927. break;
  928. case R_AARCH64_LDST64_ABS_LO12_NC:
  929. grub_arm64_set_abs_lo12_ldst64 ((grub_uint32_t *) target,
  930. sym_addr);
  931. break;
  932. case R_AARCH64_JUMP26:
  933. case R_AARCH64_CALL26:
  934. {
  935. sym_addr -= offset;
  936. sym_addr -= target_section_addr + image_target->vaddr_offset;
  937. if (!grub_arm_64_check_xxxx26_offset (sym_addr))
  938. grub_util_error ("%s", "CALL26 Relocation out of range");
  939. grub_arm64_set_xxxx26_offset((grub_uint32_t *)target,
  940. sym_addr);
  941. }
  942. break;
  943. case R_AARCH64_ADR_GOT_PAGE:
  944. {
  945. Elf64_Rela *rel2;
  946. grub_int64_t gpoffset = (((char *) gpptr - (char *) pe_target + image_target->vaddr_offset) & ~0xfffULL)
  947. - ((offset + target_section_addr + image_target->vaddr_offset) & ~0xfffULL);
  948. unsigned k;
  949. *gpptr = grub_host_to_target64 (sym_addr);
  950. unmatched_adr_got_page++;
  951. if (!grub_arm64_check_hi21_signed (gpoffset))
  952. grub_util_error ("HI21 out of range");
  953. grub_arm64_set_hi21((grub_uint32_t *)target,
  954. gpoffset);
  955. for (k = 0, rel2 = (Elf_Rela *) ((char *) r + r_size);
  956. k < num_rs;
  957. k++, rel2 = (Elf_Rela *) ((char *) rel2 + r_size))
  958. if (ELF_R_SYM (rel2->r_info)
  959. == ELF_R_SYM (r->r_info)
  960. && r->r_addend == rel2->r_addend
  961. && ELF_R_TYPE (rel2->r_info) == R_AARCH64_LD64_GOT_LO12_NC)
  962. {
  963. grub_arm64_set_abs_lo12_ldst64 ((grub_uint32_t *) SUFFIX (get_target_address) (e, target_section,
  964. grub_target_to_host (rel2->r_offset), image_target),
  965. ((char *) gpptr - (char *) pe_target + image_target->vaddr_offset));
  966. break;
  967. }
  968. if (k >= num_rs)
  969. grub_util_error ("ADR_GOT_PAGE without matching LD64_GOT_LO12_NC");
  970. gpptr++;
  971. }
  972. break;
  973. case R_AARCH64_LD64_GOT_LO12_NC:
  974. if (unmatched_adr_got_page == 0)
  975. grub_util_error ("LD64_GOT_LO12_NC without matching ADR_GOT_PAGE");
  976. unmatched_adr_got_page--;
  977. break;
  978. case R_AARCH64_ADR_PREL_PG_HI21:
  979. {
  980. sym_addr &= ~0xfffULL;
  981. sym_addr -= (offset + target_section_addr + image_target->vaddr_offset) & ~0xfffULL;
  982. if (!grub_arm64_check_hi21_signed (sym_addr))
  983. grub_util_error ("%s", "CALL26 Relocation out of range");
  984. grub_arm64_set_hi21((grub_uint32_t *)target,
  985. sym_addr);
  986. }
  987. break;
  988. default:
  989. grub_util_error (_("relocation 0x%x is not implemented yet"),
  990. (unsigned int) ELF_R_TYPE (info));
  991. break;
  992. }
  993. break;
  994. }
  995. #endif
  996. #if defined(MKIMAGE_ELF32)
  997. case EM_ARM:
  998. {
  999. sym_addr += addend;
  1000. sym_addr -= image_target->vaddr_offset;
  1001. switch (ELF_R_TYPE (info))
  1002. {
  1003. case R_ARM_ABS32:
  1004. {
  1005. grub_util_info (" ABS32:\toffset=%d\t(0x%08x)",
  1006. (int) sym_addr, (int) sym_addr);
  1007. /* Data will be naturally aligned */
  1008. if (image_target->id == IMAGE_EFI)
  1009. sym_addr += GRUB_PE32_SECTION_ALIGNMENT;
  1010. *target = grub_host_to_target32 (grub_target_to_host32 (*target) + sym_addr);
  1011. }
  1012. break;
  1013. /* Happens when compiled with -march=armv4.
  1014. Since currently we need at least armv5, keep bx as-is.
  1015. */
  1016. case R_ARM_V4BX:
  1017. break;
  1018. case R_ARM_THM_CALL:
  1019. case R_ARM_THM_JUMP24:
  1020. case R_ARM_THM_JUMP19:
  1021. {
  1022. grub_err_t err;
  1023. Elf_Sym *sym;
  1024. grub_util_info (" THM_JUMP24:\ttarget=0x%08lx\toffset=(0x%08x)",
  1025. (unsigned long) ((char *) target
  1026. - (char *) e),
  1027. sym_addr);
  1028. sym = (Elf_Sym *) ((char *) e
  1029. + grub_target_to_host (smd->symtab->sh_offset)
  1030. + ELF_R_SYM (info) * grub_target_to_host (smd->symtab->sh_entsize));
  1031. if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
  1032. sym_addr |= 1;
  1033. if (!(sym_addr & 1))
  1034. {
  1035. grub_uint32_t tr_addr;
  1036. grub_int32_t new_offset;
  1037. tr_addr = (char *) tr - (char *) pe_target
  1038. - target_section_addr;
  1039. new_offset = sym_addr - tr_addr - 12;
  1040. if (!grub_arm_jump24_check_offset (new_offset))
  1041. return grub_util_error ("jump24 relocation out of range");
  1042. tr[0] = grub_host_to_target32 (0x46c04778); /* bx pc; nop */
  1043. tr[1] = grub_host_to_target32 (((new_offset >> 2) & 0xffffff) | 0xea000000); /* b new_offset */
  1044. tr += 2;
  1045. sym_addr = tr_addr | 1;
  1046. }
  1047. sym_addr -= offset;
  1048. /* Thumb instructions can be 16-bit aligned */
  1049. if (ELF_R_TYPE (info) == R_ARM_THM_JUMP19)
  1050. err = grub_arm_reloc_thm_jump19 ((grub_uint16_t *) target, sym_addr);
  1051. else
  1052. err = grub_arm_reloc_thm_call ((grub_uint16_t *) target,
  1053. sym_addr);
  1054. if (err)
  1055. grub_util_error ("%s", grub_errmsg);
  1056. }
  1057. break;
  1058. case R_ARM_CALL:
  1059. case R_ARM_JUMP24:
  1060. {
  1061. grub_err_t err;
  1062. grub_util_info (" JUMP24:\ttarget=0x%08lx\toffset=(0x%08x)", (unsigned long) ((char *) target - (char *) e), sym_addr);
  1063. if (sym_addr & 1)
  1064. {
  1065. grub_uint32_t tr_addr;
  1066. grub_int32_t new_offset;
  1067. tr_addr = (char *) tr - (char *) pe_target
  1068. - target_section_addr;
  1069. new_offset = sym_addr - tr_addr - 12;
  1070. /* There is no immediate version of bx, only register one... */
  1071. tr[0] = grub_host_to_target32 (0xe59fc004); /* ldr ip, [pc, #4] */
  1072. tr[1] = grub_host_to_target32 (0xe08cc00f); /* add ip, ip, pc */
  1073. tr[2] = grub_host_to_target32 (0xe12fff1c); /* bx ip */
  1074. tr[3] = grub_host_to_target32 (new_offset | 1);
  1075. tr += 4;
  1076. sym_addr = tr_addr;
  1077. }
  1078. sym_addr -= offset;
  1079. err = grub_arm_reloc_jump24 (target,
  1080. sym_addr);
  1081. if (err)
  1082. grub_util_error ("%s", grub_errmsg);
  1083. }
  1084. break;
  1085. default:
  1086. grub_util_error (_("relocation 0x%x is not implemented yet"),
  1087. (unsigned int) ELF_R_TYPE (info));
  1088. break;
  1089. }
  1090. break;
  1091. }
  1092. #endif /* MKIMAGE_ELF32 */
  1093. case EM_RISCV:
  1094. {
  1095. grub_uint64_t *t64 = (grub_uint64_t *) target;
  1096. grub_uint32_t *t32 = (grub_uint32_t *) target;
  1097. grub_uint16_t *t16 = (grub_uint16_t *) target;
  1098. grub_uint8_t *t8 = (grub_uint8_t *) target;
  1099. grub_int64_t off;
  1100. /*
  1101. * Instructions and instruction encoding are documented in the RISC-V
  1102. * specification. This file is based on version 2.2:
  1103. *
  1104. * https://github.com/riscv/riscv-isa-manual/blob/master/release/riscv-spec-v2.2.pdf
  1105. */
  1106. sym_addr += addend;
  1107. off = sym_addr - target_section_addr - offset - image_target->vaddr_offset;
  1108. switch (ELF_R_TYPE (info))
  1109. {
  1110. case R_RISCV_ADD8:
  1111. *t8 = *t8 + sym_addr;
  1112. break;
  1113. case R_RISCV_ADD16:
  1114. *t16 = grub_host_to_target16 (grub_target_to_host16 (*t16) + sym_addr);
  1115. break;
  1116. case R_RISCV_32:
  1117. case R_RISCV_ADD32:
  1118. *t32 = grub_host_to_target32 (grub_target_to_host32 (*t32) + sym_addr);
  1119. break;
  1120. case R_RISCV_64:
  1121. case R_RISCV_ADD64:
  1122. *t64 = grub_host_to_target64 (grub_target_to_host64 (*t64) + sym_addr);
  1123. break;
  1124. case R_RISCV_SUB8:
  1125. *t8 = sym_addr - *t8;
  1126. break;
  1127. case R_RISCV_SUB16:
  1128. *t16 = grub_host_to_target16 (grub_target_to_host16 (*t16) - sym_addr);
  1129. break;
  1130. case R_RISCV_SUB32:
  1131. *t32 = grub_host_to_target32 (grub_target_to_host32 (*t32) - sym_addr);
  1132. break;
  1133. case R_RISCV_SUB64:
  1134. *t64 = grub_host_to_target64 (grub_target_to_host64 (*t64) - sym_addr);
  1135. break;
  1136. case R_RISCV_BRANCH:
  1137. {
  1138. grub_uint32_t imm12 = (off & 0x1000) << (31 - 12);
  1139. grub_uint32_t imm11 = (off & 0x800) >> (11 - 7);
  1140. grub_uint32_t imm10_5 = (off & 0x7e0) << (30 - 10);
  1141. grub_uint32_t imm4_1 = (off & 0x1e) << (11 - 4);
  1142. *t32 = grub_host_to_target32 ((grub_target_to_host32 (*t32) & 0x1fff07f)
  1143. | imm12 | imm11 | imm10_5 | imm4_1);
  1144. }
  1145. break;
  1146. case R_RISCV_JAL:
  1147. {
  1148. grub_uint32_t imm20 = (off & 0x100000) << (31 - 20);
  1149. grub_uint32_t imm19_12 = (off & 0xff000);
  1150. grub_uint32_t imm11 = (off & 0x800) << (20 - 11);
  1151. grub_uint32_t imm10_1 = (off & 0x7fe) << (30 - 10);
  1152. *t32 = grub_host_to_target32 ((grub_target_to_host32 (*t32) & 0xfff)
  1153. | imm20 | imm19_12 | imm11 | imm10_1);
  1154. }
  1155. break;
  1156. case R_RISCV_CALL:
  1157. {
  1158. grub_uint32_t hi20, lo12;
  1159. if (off != (grub_int32_t)off)
  1160. grub_util_error ("target %lx not reachable from pc=%lx", (long)sym_addr, (long)((char *)target - (char *)e));
  1161. hi20 = (off + 0x800) & 0xfffff000;
  1162. lo12 = (off - hi20) & 0xfff;
  1163. t32[0] = grub_host_to_target32 ((grub_target_to_host32 (t32[0]) & 0xfff) | hi20);
  1164. t32[1] = grub_host_to_target32 ((grub_target_to_host32 (t32[1]) & 0xfffff) | (lo12 << 20));
  1165. }
  1166. break;
  1167. case R_RISCV_RVC_BRANCH:
  1168. {
  1169. grub_uint16_t imm8 = (off & 0x100) << (12 - 8);
  1170. grub_uint16_t imm7_6 = (off & 0xc0) >> (6 - 5);
  1171. grub_uint16_t imm5 = (off & 0x20) >> (5 - 2);
  1172. grub_uint16_t imm4_3 = (off & 0x18) << (12 - 5);
  1173. grub_uint16_t imm2_1 = (off & 0x6) << (12 - 10);
  1174. *t16 = grub_host_to_target16 ((grub_target_to_host16 (*t16) & 0xe383)
  1175. | imm8 | imm7_6 | imm5 | imm4_3 | imm2_1);
  1176. }
  1177. break;
  1178. case R_RISCV_RVC_JUMP:
  1179. {
  1180. grub_uint16_t imm11 = (off & 0x800) << (12 - 11);
  1181. grub_uint16_t imm10 = (off & 0x400) >> (10 - 8);
  1182. grub_uint16_t imm9_8 = (off & 0x300) << (12 - 11);
  1183. grub_uint16_t imm7 = (off & 0x80) >> (7 - 6);
  1184. grub_uint16_t imm6 = (off & 0x40) << (12 - 11);
  1185. grub_uint16_t imm5 = (off & 0x20) >> (5 - 2);
  1186. grub_uint16_t imm4 = (off & 0x10) << (12 - 5);
  1187. grub_uint16_t imm3_1 = (off & 0xe) << (12 - 10);
  1188. *t16 = grub_host_to_target16 ((grub_target_to_host16 (*t16) & 0xe003)
  1189. | imm11 | imm10 | imm9_8 | imm7 | imm6
  1190. | imm5 | imm4 | imm3_1);
  1191. }
  1192. break;
  1193. case R_RISCV_PCREL_HI20:
  1194. {
  1195. grub_int32_t hi20;
  1196. if (off != (grub_int32_t)off)
  1197. grub_util_error ("target %lx not reachable from pc=%lx", (long)sym_addr, (long)((char *)target - (char *)e));
  1198. hi20 = (off + 0x800) & 0xfffff000;
  1199. *t32 = grub_host_to_target32 ((grub_target_to_host32 (*t32) & 0xfff) | hi20);
  1200. }
  1201. break;
  1202. case R_RISCV_PCREL_LO12_I:
  1203. case R_RISCV_PCREL_LO12_S:
  1204. {
  1205. Elf_Rela *rel2;
  1206. Elf_Word k;
  1207. /* Search backwards for matching HI20 reloc. */
  1208. for (k = j, rel2 = (Elf_Rela *) ((char *) r - r_size);
  1209. k > 0;
  1210. k--, rel2 = (Elf_Rela *) ((char *) rel2 - r_size))
  1211. {
  1212. Elf_Addr rel2_info;
  1213. Elf_Addr rel2_offset;
  1214. Elf_Addr rel2_sym_addr;
  1215. Elf_Addr rel2_addend;
  1216. Elf_Addr rel2_loc;
  1217. grub_int64_t rel2_off;
  1218. rel2_offset = grub_target_to_host (rel2->r_offset);
  1219. rel2_info = grub_target_to_host (rel2->r_info);
  1220. rel2_loc = target_section_addr + rel2_offset + image_target->vaddr_offset;
  1221. if (ELF_R_TYPE (rel2_info) == R_RISCV_PCREL_HI20
  1222. && rel2_loc == sym_addr)
  1223. {
  1224. rel2_sym_addr = SUFFIX (get_symbol_address)
  1225. (e, smd->symtab, ELF_R_SYM (rel2_info),
  1226. image_target);
  1227. rel2_addend = (s->sh_type == grub_target_to_host32 (SHT_RELA)) ?
  1228. grub_target_to_host (rel2->r_addend) : 0;
  1229. rel2_off = rel2_sym_addr + rel2_addend - rel2_loc;
  1230. off = rel2_off - ((rel2_off + 0x800) & 0xfffff000);
  1231. if (ELF_R_TYPE (info) == R_RISCV_PCREL_LO12_I)
  1232. *t32 = grub_host_to_target32 ((grub_target_to_host32 (*t32) & 0xfffff) | (off & 0xfff) << 20);
  1233. else
  1234. {
  1235. grub_uint32_t imm11_5 = (off & 0xfe0) << (31 - 11);
  1236. grub_uint32_t imm4_0 = (off & 0x1f) << (11 - 4);
  1237. *t32 = grub_host_to_target32 ((grub_target_to_host32 (*t32) & 0x1fff07f) | imm11_5 | imm4_0);
  1238. }
  1239. break;
  1240. }
  1241. }
  1242. if (k == 0)
  1243. grub_util_error ("cannot find matching HI20 relocation");
  1244. }
  1245. break;
  1246. case R_RISCV_HI20:
  1247. *t32 = grub_host_to_target32 ((grub_target_to_host32 (*t32) & 0xfff) | (((grub_int32_t) sym_addr + 0x800) & 0xfffff000));
  1248. break;
  1249. case R_RISCV_LO12_I:
  1250. {
  1251. grub_int32_t lo12 = (grub_int32_t) sym_addr - (((grub_int32_t) sym_addr + 0x800) & 0xfffff000);
  1252. *t32 = grub_host_to_target32 ((grub_target_to_host32 (*t32) & 0xfffff) | ((lo12 & 0xfff) << 20));
  1253. }
  1254. break;
  1255. case R_RISCV_LO12_S:
  1256. {
  1257. grub_int32_t lo12 = (grub_int32_t) sym_addr - (((grub_int32_t) sym_addr + 0x800) & 0xfffff000);
  1258. grub_uint32_t imm11_5 = (lo12 & 0xfe0) << (31 - 11);
  1259. grub_uint32_t imm4_0 = (lo12 & 0x1f) << (11 - 4);
  1260. *t32 = grub_host_to_target32 ((grub_target_to_host32 (*t32) & 0x1fff07f) | imm11_5 | imm4_0);
  1261. }
  1262. break;
  1263. case R_RISCV_RELAX:
  1264. break;
  1265. default:
  1266. grub_util_error (_("relocation 0x%x is not implemented yet"),
  1267. (unsigned int) ELF_R_TYPE (info));
  1268. break;
  1269. }
  1270. break;
  1271. }
  1272. default:
  1273. grub_util_error ("unknown architecture type %d",
  1274. image_target->elf_target);
  1275. }
  1276. }
  1277. }
  1278. }
  1279. /* Add a PE32's fixup entry for a relocation. Return the resulting address
  1280. after having written to the file OUT. */
  1281. static Elf_Addr
  1282. add_fixup_entry (struct fixup_block_list **cblock, grub_uint16_t type,
  1283. Elf_Addr addr, int flush, Elf_Addr current_address,
  1284. const struct grub_install_image_target_desc *image_target)
  1285. {
  1286. struct grub_pe32_fixup_block *b;
  1287. b = &((*cblock)->b);
  1288. /* First, check if it is necessary to write out the current block. */
  1289. if ((*cblock)->state)
  1290. {
  1291. if (flush || addr < b->page_rva || b->page_rva + 0x1000 <= addr)
  1292. {
  1293. grub_uint32_t size;
  1294. if (flush)
  1295. {
  1296. /* Add as much padding as necessary to align the address
  1297. with a section boundary. */
  1298. Elf_Addr next_address;
  1299. unsigned padding_size;
  1300. size_t cur_index;
  1301. next_address = current_address + b->block_size;
  1302. padding_size = ((ALIGN_UP (next_address, image_target->section_align)
  1303. - next_address)
  1304. >> 1);
  1305. cur_index = ((b->block_size - sizeof (*b)) >> 1);
  1306. grub_util_info ("adding %d padding fixup entries", padding_size);
  1307. while (padding_size--)
  1308. {
  1309. b->entries[cur_index++] = 0;
  1310. b->block_size += 2;
  1311. }
  1312. }
  1313. else while (b->block_size & (8 - 1))
  1314. {
  1315. /* If not aligned with a 32-bit boundary, add
  1316. a padding entry. */
  1317. size_t cur_index;
  1318. grub_util_info ("adding a padding fixup entry");
  1319. cur_index = ((b->block_size - sizeof (*b)) >> 1);
  1320. b->entries[cur_index] = 0;
  1321. b->block_size += 2;
  1322. }
  1323. /* Flush it. */
  1324. grub_util_info ("writing %d bytes of a fixup block starting at 0x%x",
  1325. b->block_size, b->page_rva);
  1326. size = b->block_size;
  1327. current_address += size;
  1328. b->page_rva = grub_host_to_target32 (b->page_rva);
  1329. b->block_size = grub_host_to_target32 (b->block_size);
  1330. (*cblock)->next = xmalloc (sizeof (**cblock) + 2 * 0x1000);
  1331. memset ((*cblock)->next, 0, sizeof (**cblock) + 2 * 0x1000);
  1332. *cblock = (*cblock)->next;
  1333. }
  1334. }
  1335. b = &((*cblock)->b);
  1336. if (! flush)
  1337. {
  1338. grub_uint16_t entry;
  1339. size_t cur_index;
  1340. /* If not allocated yet, allocate a block with enough entries. */
  1341. if (! (*cblock)->state)
  1342. {
  1343. (*cblock)->state = 1;
  1344. /* The spec does not mention the requirement of a Page RVA.
  1345. Here, align the address with a 4K boundary for safety. */
  1346. b->page_rva = (addr & ~(0x1000 - 1));
  1347. b->block_size = sizeof (*b);
  1348. }
  1349. /* Sanity check. */
  1350. if (b->block_size >= sizeof (*b) + 2 * 0x1000)
  1351. grub_util_error ("too many fixup entries");
  1352. /* Add a new entry. */
  1353. cur_index = ((b->block_size - sizeof (*b)) >> 1);
  1354. entry = GRUB_PE32_FIXUP_ENTRY (type, addr - b->page_rva);
  1355. b->entries[cur_index] = grub_host_to_target16 (entry);
  1356. b->block_size += 2;
  1357. }
  1358. return current_address;
  1359. }
  1360. struct raw_reloc
  1361. {
  1362. struct raw_reloc *next;
  1363. grub_uint32_t offset;
  1364. enum raw_reloc_type {
  1365. RAW_RELOC_NONE = -1,
  1366. RAW_RELOC_32 = 0,
  1367. RAW_RELOC_MAX = 1,
  1368. } type;
  1369. };
  1370. struct translate_context
  1371. {
  1372. /* PE */
  1373. struct fixup_block_list *lst, *lst0;
  1374. Elf_Addr current_address;
  1375. /* Raw */
  1376. struct raw_reloc *raw_relocs;
  1377. };
  1378. static void
  1379. translate_reloc_start (struct translate_context *ctx,
  1380. const struct grub_install_image_target_desc *image_target)
  1381. {
  1382. grub_memset (ctx, 0, sizeof (*ctx));
  1383. if (image_target->id == IMAGE_EFI)
  1384. {
  1385. ctx->lst = ctx->lst0 = xmalloc (sizeof (*ctx->lst) + 2 * 0x1000);
  1386. memset (ctx->lst, 0, sizeof (*ctx->lst) + 2 * 0x1000);
  1387. ctx->current_address = 0;
  1388. }
  1389. }
  1390. static void
  1391. translate_relocation_pe (struct translate_context *ctx,
  1392. Elf_Addr addr,
  1393. Elf_Addr info,
  1394. const struct grub_install_image_target_desc *image_target)
  1395. {
  1396. /* Necessary to relocate only absolute addresses. */
  1397. switch (image_target->elf_target)
  1398. {
  1399. case EM_386:
  1400. if (ELF_R_TYPE (info) == R_386_32)
  1401. {
  1402. grub_util_info ("adding a relocation entry for 0x%"
  1403. GRUB_HOST_PRIxLONG_LONG,
  1404. (unsigned long long) addr);
  1405. ctx->current_address
  1406. = add_fixup_entry (&ctx->lst,
  1407. GRUB_PE32_REL_BASED_HIGHLOW,
  1408. addr, 0, ctx->current_address,
  1409. image_target);
  1410. }
  1411. break;
  1412. case EM_X86_64:
  1413. if ((ELF_R_TYPE (info) == R_X86_64_32) ||
  1414. (ELF_R_TYPE (info) == R_X86_64_32S))
  1415. {
  1416. grub_util_error ("can\'t add fixup entry for R_X86_64_32(S)");
  1417. }
  1418. else if (ELF_R_TYPE (info) == R_X86_64_64)
  1419. {
  1420. grub_util_info ("adding a relocation entry for 0x%"
  1421. GRUB_HOST_PRIxLONG_LONG,
  1422. (unsigned long long) addr);
  1423. ctx->current_address
  1424. = add_fixup_entry (&ctx->lst,
  1425. GRUB_PE32_REL_BASED_DIR64,
  1426. addr,
  1427. 0, ctx->current_address,
  1428. image_target);
  1429. }
  1430. break;
  1431. case EM_IA_64:
  1432. switch (ELF_R_TYPE (info))
  1433. {
  1434. case R_IA64_PCREL64LSB:
  1435. case R_IA64_LDXMOV:
  1436. case R_IA64_PCREL21B:
  1437. case R_IA64_LTOFF_FPTR22:
  1438. case R_IA64_LTOFF22X:
  1439. case R_IA64_LTOFF22:
  1440. case R_IA64_GPREL22:
  1441. case R_IA64_GPREL64I:
  1442. case R_IA64_SEGREL64LSB:
  1443. break;
  1444. case R_IA64_FPTR64LSB:
  1445. case R_IA64_DIR64LSB:
  1446. #if 1
  1447. {
  1448. grub_util_info ("adding a relocation entry for 0x%"
  1449. GRUB_HOST_PRIxLONG_LONG,
  1450. (unsigned long long) addr);
  1451. ctx->current_address
  1452. = add_fixup_entry (&ctx->lst,
  1453. GRUB_PE32_REL_BASED_DIR64,
  1454. addr,
  1455. 0, ctx->current_address,
  1456. image_target);
  1457. }
  1458. #endif
  1459. break;
  1460. default:
  1461. grub_util_error (_("relocation 0x%x is not implemented yet"),
  1462. (unsigned int) ELF_R_TYPE (info));
  1463. break;
  1464. }
  1465. break;
  1466. case EM_AARCH64:
  1467. switch (ELF_R_TYPE (info))
  1468. {
  1469. case R_AARCH64_ABS64:
  1470. {
  1471. ctx->current_address
  1472. = add_fixup_entry (&ctx->lst,
  1473. GRUB_PE32_REL_BASED_DIR64,
  1474. addr, 0, ctx->current_address,
  1475. image_target);
  1476. }
  1477. break;
  1478. /* Relative relocations do not require fixup entries. */
  1479. case R_AARCH64_CALL26:
  1480. case R_AARCH64_JUMP26:
  1481. case R_AARCH64_PREL32:
  1482. break;
  1483. /* Page-relative relocations do not require fixup entries. */
  1484. case R_AARCH64_ADR_PREL_PG_HI21:
  1485. /* We page-align the whole kernel, so no need
  1486. for fixup entries.
  1487. */
  1488. case R_AARCH64_ADD_ABS_LO12_NC:
  1489. case R_AARCH64_LDST64_ABS_LO12_NC:
  1490. break;
  1491. /* GOT is relocated separately. */
  1492. case R_AARCH64_ADR_GOT_PAGE:
  1493. case R_AARCH64_LD64_GOT_LO12_NC:
  1494. break;
  1495. default:
  1496. grub_util_error (_("relocation 0x%x is not implemented yet"),
  1497. (unsigned int) ELF_R_TYPE (info));
  1498. break;
  1499. }
  1500. break;
  1501. break;
  1502. #if defined(MKIMAGE_ELF32)
  1503. case EM_ARM:
  1504. switch (ELF_R_TYPE (info))
  1505. {
  1506. case R_ARM_V4BX:
  1507. /* Relative relocations do not require fixup entries. */
  1508. case R_ARM_JUMP24:
  1509. case R_ARM_THM_CALL:
  1510. case R_ARM_THM_JUMP19:
  1511. case R_ARM_THM_JUMP24:
  1512. case R_ARM_CALL:
  1513. {
  1514. grub_util_info (" %s: not adding fixup: 0x%08x : 0x%08x", __FUNCTION__, (unsigned int) addr, (unsigned int) ctx->current_address);
  1515. }
  1516. break;
  1517. /* Create fixup entry for PE/COFF loader */
  1518. case R_ARM_ABS32:
  1519. {
  1520. ctx->current_address
  1521. = add_fixup_entry (&ctx->lst,
  1522. GRUB_PE32_REL_BASED_HIGHLOW,
  1523. addr, 0, ctx->current_address,
  1524. image_target);
  1525. }
  1526. break;
  1527. default:
  1528. grub_util_error (_("relocation 0x%x is not implemented yet"),
  1529. (unsigned int) ELF_R_TYPE (info));
  1530. break;
  1531. }
  1532. break;
  1533. #endif /* defined(MKIMAGE_ELF32) */
  1534. case EM_RISCV:
  1535. switch (ELF_R_TYPE (info))
  1536. {
  1537. case R_RISCV_32:
  1538. {
  1539. ctx->current_address
  1540. = add_fixup_entry (&ctx->lst,
  1541. GRUB_PE32_REL_BASED_HIGHLOW,
  1542. addr, 0, ctx->current_address,
  1543. image_target);
  1544. }
  1545. break;
  1546. case R_RISCV_64:
  1547. {
  1548. ctx->current_address
  1549. = add_fixup_entry (&ctx->lst,
  1550. GRUB_PE32_REL_BASED_DIR64,
  1551. addr, 0, ctx->current_address,
  1552. image_target);
  1553. }
  1554. break;
  1555. /* Relative relocations do not require fixup entries. */
  1556. case R_RISCV_BRANCH:
  1557. case R_RISCV_JAL:
  1558. case R_RISCV_CALL:
  1559. case R_RISCV_PCREL_HI20:
  1560. case R_RISCV_PCREL_LO12_I:
  1561. case R_RISCV_PCREL_LO12_S:
  1562. case R_RISCV_RVC_BRANCH:
  1563. case R_RISCV_RVC_JUMP:
  1564. case R_RISCV_ADD32:
  1565. case R_RISCV_SUB32:
  1566. grub_util_info (" %s: not adding fixup: 0x%08x : 0x%08x", __FUNCTION__, (unsigned int) addr, (unsigned int) ctx->current_address);
  1567. break;
  1568. case R_RISCV_HI20:
  1569. {
  1570. ctx->current_address
  1571. = add_fixup_entry (&ctx->lst,
  1572. GRUB_PE32_REL_BASED_RISCV_HI20,
  1573. addr, 0, ctx->current_address,
  1574. image_target);
  1575. }
  1576. break;
  1577. case R_RISCV_LO12_I:
  1578. {
  1579. ctx->current_address
  1580. = add_fixup_entry (&ctx->lst,
  1581. GRUB_PE32_REL_BASED_RISCV_LOW12I,
  1582. addr, 0, ctx->current_address,
  1583. image_target);
  1584. }
  1585. break;
  1586. case R_RISCV_LO12_S:
  1587. {
  1588. ctx->current_address
  1589. = add_fixup_entry (&ctx->lst,
  1590. GRUB_PE32_REL_BASED_RISCV_LOW12S,
  1591. addr, 0, ctx->current_address,
  1592. image_target);
  1593. }
  1594. break;
  1595. case R_RISCV_RELAX:
  1596. break;
  1597. default:
  1598. grub_util_error (_("relocation 0x%x is not implemented yet"),
  1599. (unsigned int) ELF_R_TYPE (info));
  1600. break;
  1601. }
  1602. break;
  1603. default:
  1604. grub_util_error ("unknown machine type 0x%x", image_target->elf_target);
  1605. }
  1606. }
  1607. static enum raw_reloc_type
  1608. classify_raw_reloc (Elf_Addr info,
  1609. const struct grub_install_image_target_desc *image_target)
  1610. {
  1611. /* Necessary to relocate only absolute addresses. */
  1612. switch (image_target->elf_target)
  1613. {
  1614. case EM_ARM:
  1615. switch (ELF_R_TYPE (info))
  1616. {
  1617. case R_ARM_V4BX:
  1618. case R_ARM_JUMP24:
  1619. case R_ARM_THM_CALL:
  1620. case R_ARM_THM_JUMP19:
  1621. case R_ARM_THM_JUMP24:
  1622. case R_ARM_CALL:
  1623. return RAW_RELOC_NONE;
  1624. case R_ARM_ABS32:
  1625. return RAW_RELOC_32;
  1626. default:
  1627. grub_util_error (_("relocation 0x%x is not implemented yet"),
  1628. (unsigned int) ELF_R_TYPE (info));
  1629. break;
  1630. }
  1631. break;
  1632. default:
  1633. grub_util_error ("unknown machine type 0x%x", image_target->elf_target);
  1634. }
  1635. }
  1636. static void
  1637. translate_relocation_raw (struct translate_context *ctx,
  1638. Elf_Addr addr,
  1639. Elf_Addr info,
  1640. const struct grub_install_image_target_desc *image_target)
  1641. {
  1642. enum raw_reloc_type class = classify_raw_reloc (info, image_target);
  1643. struct raw_reloc *rel;
  1644. if (class == RAW_RELOC_NONE)
  1645. return;
  1646. rel = xmalloc (sizeof (*rel));
  1647. rel->next = ctx->raw_relocs;
  1648. rel->type = class;
  1649. rel->offset = addr;
  1650. ctx->raw_relocs = rel;
  1651. }
  1652. static void
  1653. translate_relocation (struct translate_context *ctx,
  1654. Elf_Addr addr,
  1655. Elf_Addr info,
  1656. const struct grub_install_image_target_desc *image_target)
  1657. {
  1658. if (image_target->id == IMAGE_EFI)
  1659. translate_relocation_pe (ctx, addr, info, image_target);
  1660. else
  1661. translate_relocation_raw (ctx, addr, info, image_target);
  1662. }
  1663. static void
  1664. finish_reloc_translation_pe (struct translate_context *ctx, struct grub_mkimage_layout *layout,
  1665. const struct grub_install_image_target_desc *image_target)
  1666. {
  1667. ctx->current_address = add_fixup_entry (&ctx->lst, 0, 0, 1, ctx->current_address, image_target);
  1668. {
  1669. grub_uint8_t *ptr;
  1670. layout->reloc_section = ptr = xmalloc (ctx->current_address);
  1671. for (ctx->lst = ctx->lst0; ctx->lst; ctx->lst = ctx->lst->next)
  1672. if (ctx->lst->state)
  1673. {
  1674. memcpy (ptr, &ctx->lst->b, grub_target_to_host32 (ctx->lst->b.block_size));
  1675. ptr += grub_target_to_host32 (ctx->lst->b.block_size);
  1676. }
  1677. assert ((ctx->current_address + (grub_uint8_t *) layout->reloc_section) == ptr);
  1678. }
  1679. for (ctx->lst = ctx->lst0; ctx->lst; )
  1680. {
  1681. struct fixup_block_list *next;
  1682. next = ctx->lst->next;
  1683. free (ctx->lst);
  1684. ctx->lst = next;
  1685. }
  1686. layout->reloc_size = ctx->current_address;
  1687. if (image_target->elf_target == EM_ARM && layout->reloc_size > GRUB_KERNEL_ARM_STACK_SIZE)
  1688. grub_util_error ("Reloc section (%d) is bigger than stack size (%d). "
  1689. "This breaks assembly assumptions. Please increase stack size",
  1690. (int) layout->reloc_size,
  1691. (int) GRUB_KERNEL_ARM_STACK_SIZE);
  1692. }
  1693. /*
  1694. Layout:
  1695. <type 0 relocations>
  1696. <fffffffe>
  1697. <type 1 relocations>
  1698. <fffffffe>
  1699. ...
  1700. <type n relocations>
  1701. <ffffffff>
  1702. each relocation starts with 32-bit offset. Rest depends on relocation.
  1703. mkimage stops when it sees first unknown type or end marker.
  1704. This allows images to be created with mismatched mkimage and
  1705. kernel as long as no relocations are present in kernel that mkimage
  1706. isn't aware of (in which case mkimage aborts).
  1707. This also allows simple assembly to do the relocs.
  1708. */
  1709. #define RAW_SEPARATOR 0xfffffffe
  1710. #define RAW_END_MARKER 0xffffffff
  1711. static void
  1712. finish_reloc_translation_raw (struct translate_context *ctx, struct grub_mkimage_layout *layout,
  1713. const struct grub_install_image_target_desc *image_target)
  1714. {
  1715. size_t count = 0, sz;
  1716. enum raw_reloc_type highest = RAW_RELOC_NONE;
  1717. enum raw_reloc_type curtype;
  1718. struct raw_reloc *cur;
  1719. grub_uint32_t *p;
  1720. if (!ctx->raw_relocs)
  1721. {
  1722. layout->reloc_section = p = xmalloc (sizeof (grub_uint32_t));
  1723. p[0] = RAW_END_MARKER;
  1724. layout->reloc_size = sizeof (grub_uint32_t);
  1725. return;
  1726. }
  1727. for (cur = ctx->raw_relocs; cur; cur = cur->next)
  1728. {
  1729. count++;
  1730. if (cur->type > highest)
  1731. highest = cur->type;
  1732. }
  1733. /* highest separators, count relocations and one end marker. */
  1734. sz = (highest + count + 1) * sizeof (grub_uint32_t);
  1735. layout->reloc_section = p = xmalloc (sz);
  1736. for (curtype = 0; curtype <= highest; curtype++)
  1737. {
  1738. /* Support for special cases would go here. */
  1739. for (cur = ctx->raw_relocs; cur; cur = cur->next)
  1740. if (cur->type == curtype)
  1741. {
  1742. *p++ = cur->offset;
  1743. }
  1744. *p++ = RAW_SEPARATOR;
  1745. }
  1746. *--p = RAW_END_MARKER;
  1747. layout->reloc_size = sz;
  1748. }
  1749. static void
  1750. finish_reloc_translation (struct translate_context *ctx, struct grub_mkimage_layout *layout,
  1751. const struct grub_install_image_target_desc *image_target)
  1752. {
  1753. if (image_target->id == IMAGE_EFI)
  1754. finish_reloc_translation_pe (ctx, layout, image_target);
  1755. else
  1756. finish_reloc_translation_raw (ctx, layout, image_target);
  1757. }
  1758. static void
  1759. create_u64_fixups (struct translate_context *ctx,
  1760. Elf_Addr jumpers, grub_size_t njumpers,
  1761. const struct grub_install_image_target_desc *image_target)
  1762. {
  1763. unsigned i;
  1764. assert (image_target->id == IMAGE_EFI);
  1765. for (i = 0; i < njumpers; i++)
  1766. ctx->current_address = add_fixup_entry (&ctx->lst,
  1767. GRUB_PE32_REL_BASED_DIR64,
  1768. jumpers + 8 * i,
  1769. 0, ctx->current_address,
  1770. image_target);
  1771. }
  1772. /* Make a .reloc section. */
  1773. static void
  1774. make_reloc_section (Elf_Ehdr *e, struct grub_mkimage_layout *layout,
  1775. struct section_metadata *smd,
  1776. const struct grub_install_image_target_desc *image_target)
  1777. {
  1778. unsigned i;
  1779. Elf_Shdr *s;
  1780. struct translate_context ctx;
  1781. translate_reloc_start (&ctx, image_target);
  1782. for (i = 0, s = smd->sections; i < smd->num_sections;
  1783. i++, s = (Elf_Shdr *) ((char *) s + smd->section_entsize))
  1784. if ((grub_target_to_host32 (s->sh_type) == SHT_REL) ||
  1785. (grub_target_to_host32 (s->sh_type) == SHT_RELA))
  1786. {
  1787. Elf_Rel *r;
  1788. Elf_Word rtab_size, r_size, num_rs;
  1789. Elf_Off rtab_offset;
  1790. Elf_Addr section_address;
  1791. Elf_Word j;
  1792. if (!SUFFIX (is_kept_reloc_section) (s, image_target, smd))
  1793. {
  1794. grub_util_info ("not translating the skipped relocation section %s",
  1795. smd->strtab + grub_le_to_cpu32 (s->sh_name));
  1796. continue;
  1797. }
  1798. grub_util_info ("translating the relocation section %s",
  1799. smd->strtab + grub_le_to_cpu32 (s->sh_name));
  1800. rtab_size = grub_target_to_host (s->sh_size);
  1801. r_size = grub_target_to_host (s->sh_entsize);
  1802. rtab_offset = grub_target_to_host (s->sh_offset);
  1803. num_rs = rtab_size / r_size;
  1804. section_address = smd->vaddrs[grub_le_to_cpu32 (s->sh_info)];
  1805. for (j = 0, r = (Elf_Rel *) ((char *) e + rtab_offset);
  1806. j < num_rs;
  1807. j++, r = (Elf_Rel *) ((char *) r + r_size))
  1808. {
  1809. Elf_Addr info;
  1810. Elf_Addr offset;
  1811. Elf_Addr addr;
  1812. offset = grub_target_to_host (r->r_offset);
  1813. info = grub_target_to_host (r->r_info);
  1814. addr = section_address + offset;
  1815. translate_relocation (&ctx, addr, info, image_target);
  1816. }
  1817. }
  1818. if (image_target->elf_target == EM_IA_64)
  1819. create_u64_fixups (&ctx,
  1820. layout->ia64jmp_off
  1821. + image_target->vaddr_offset,
  1822. 2 * layout->ia64jmpnum,
  1823. image_target);
  1824. if (image_target->elf_target == EM_IA_64 || image_target->elf_target == EM_AARCH64)
  1825. create_u64_fixups (&ctx,
  1826. layout->got_off
  1827. + image_target->vaddr_offset,
  1828. (layout->got_size / 8),
  1829. image_target);
  1830. finish_reloc_translation (&ctx, layout, image_target);
  1831. }
  1832. /* Determine if this section is a text section. Return false if this
  1833. section is not allocated. */
  1834. static int
  1835. SUFFIX (is_text_section) (Elf_Shdr *s, const struct grub_install_image_target_desc *image_target)
  1836. {
  1837. if (!is_relocatable (image_target)
  1838. && grub_target_to_host32 (s->sh_type) != SHT_PROGBITS)
  1839. return 0;
  1840. return ((grub_target_to_host (s->sh_flags) & (SHF_EXECINSTR | SHF_ALLOC))
  1841. == (SHF_EXECINSTR | SHF_ALLOC));
  1842. }
  1843. /* Determine if this section is a data section. */
  1844. static int
  1845. SUFFIX (is_data_section) (Elf_Shdr *s, const struct grub_install_image_target_desc *image_target)
  1846. {
  1847. if (!is_relocatable (image_target)
  1848. && grub_target_to_host32 (s->sh_type) != SHT_PROGBITS)
  1849. return 0;
  1850. return ((grub_target_to_host (s->sh_flags) & (SHF_EXECINSTR | SHF_ALLOC))
  1851. == SHF_ALLOC) && !(grub_target_to_host32 (s->sh_type) == SHT_NOBITS);
  1852. }
  1853. static int
  1854. SUFFIX (is_bss_section) (Elf_Shdr *s, const struct grub_install_image_target_desc *image_target)
  1855. {
  1856. if (!is_relocatable (image_target))
  1857. return 0;
  1858. return ((grub_target_to_host (s->sh_flags) & (SHF_EXECINSTR | SHF_ALLOC))
  1859. == SHF_ALLOC) && (grub_target_to_host32 (s->sh_type) == SHT_NOBITS);
  1860. }
  1861. /* Determine if a section is going to be in the final output */
  1862. static int
  1863. SUFFIX (is_kept_section) (Elf_Shdr *s, const struct grub_install_image_target_desc *image_target)
  1864. {
  1865. /* We keep .text and .data */
  1866. if (SUFFIX (is_text_section) (s, image_target)
  1867. || SUFFIX (is_data_section) (s, image_target))
  1868. return 1;
  1869. /*
  1870. * And we keep .bss if we're producing PE binaries or the target doesn't
  1871. * have a relocating loader. Platforms other than EFI and U-boot shouldn't
  1872. * have .bss in their binaries as we build with -Wl,-Ttext.
  1873. */
  1874. if (SUFFIX (is_bss_section) (s, image_target)
  1875. && (image_target->id == IMAGE_EFI || !is_relocatable (image_target)))
  1876. return 1;
  1877. /* Otherwise this is not a section we're keeping in the final output. */
  1878. return 0;
  1879. }
  1880. static int
  1881. SUFFIX (is_kept_reloc_section) (Elf_Shdr *s, const struct grub_install_image_target_desc *image_target,
  1882. struct section_metadata *smd)
  1883. {
  1884. int i;
  1885. int r = 0;
  1886. const char *name = smd->strtab + grub_host_to_target32 (s->sh_name);
  1887. if (!strncmp (name, ".rela.", 6))
  1888. name += 5;
  1889. else if (!strncmp (name, ".rel.", 5))
  1890. name += 4;
  1891. else
  1892. return 1;
  1893. for (i = 0, s = smd->sections; i < smd->num_sections;
  1894. i++, s = (Elf_Shdr *) ((char *) s + smd->section_entsize))
  1895. {
  1896. const char *sname = smd->strtab + grub_host_to_target32 (s->sh_name);
  1897. if (strcmp (sname, name))
  1898. continue;
  1899. return SUFFIX (is_kept_section) (s, image_target);
  1900. }
  1901. return r;
  1902. }
  1903. /* Return if the ELF header is valid. */
  1904. static int
  1905. SUFFIX (check_elf_header) (Elf_Ehdr *e, size_t size, const struct grub_install_image_target_desc *image_target)
  1906. {
  1907. if (size < sizeof (*e)
  1908. || e->e_ident[EI_MAG0] != ELFMAG0
  1909. || e->e_ident[EI_MAG1] != ELFMAG1
  1910. || e->e_ident[EI_MAG2] != ELFMAG2
  1911. || e->e_ident[EI_MAG3] != ELFMAG3
  1912. || e->e_ident[EI_VERSION] != EV_CURRENT
  1913. || e->e_ident[EI_CLASS] != ELFCLASSXX
  1914. || e->e_version != grub_host_to_target32 (EV_CURRENT))
  1915. return 0;
  1916. return 1;
  1917. }
  1918. static Elf_Addr
  1919. SUFFIX (put_section) (Elf_Shdr *s, int i,
  1920. Elf_Addr current_address,
  1921. struct section_metadata *smd,
  1922. const struct grub_install_image_target_desc *image_target)
  1923. {
  1924. Elf_Word align = grub_host_to_target_addr (s->sh_addralign);
  1925. const char *name = smd->strtab + grub_host_to_target32 (s->sh_name);
  1926. if (align)
  1927. current_address = ALIGN_UP (current_address + image_target->vaddr_offset,
  1928. align)
  1929. - image_target->vaddr_offset;
  1930. grub_util_info ("locating the section %s at 0x%"
  1931. GRUB_HOST_PRIxLONG_LONG,
  1932. name, (unsigned long long) current_address);
  1933. if (!is_relocatable (image_target))
  1934. current_address = grub_host_to_target_addr (s->sh_addr)
  1935. - image_target->link_addr;
  1936. smd->addrs[i] = current_address;
  1937. current_address += grub_host_to_target_addr (s->sh_size);
  1938. return current_address;
  1939. }
  1940. /*
  1941. * Locate section addresses by merging code sections and data sections
  1942. * into .text and .data, respectively.
  1943. */
  1944. static void
  1945. SUFFIX (locate_sections) (Elf_Ehdr *e, const char *kernel_path,
  1946. struct section_metadata *smd,
  1947. struct grub_mkimage_layout *layout,
  1948. const struct grub_install_image_target_desc *image_target)
  1949. {
  1950. int i;
  1951. Elf_Shdr *s;
  1952. layout->align = 1;
  1953. /* Page-aligning simplifies relocation handling. */
  1954. if (image_target->elf_target == EM_AARCH64)
  1955. layout->align = 4096;
  1956. layout->kernel_size = 0;
  1957. for (i = 0, s = smd->sections;
  1958. i < smd->num_sections;
  1959. i++, s = (Elf_Shdr *) ((char *) s + smd->section_entsize))
  1960. if ((grub_target_to_host (s->sh_flags) & SHF_ALLOC)
  1961. && grub_host_to_target32 (s->sh_addralign) > layout->align)
  1962. layout->align = grub_host_to_target32 (s->sh_addralign);
  1963. /* .text */
  1964. for (i = 0, s = smd->sections;
  1965. i < smd->num_sections;
  1966. i++, s = (Elf_Shdr *) ((char *) s + smd->section_entsize))
  1967. if (SUFFIX (is_text_section) (s, image_target))
  1968. {
  1969. layout->kernel_size = SUFFIX (put_section) (s, i, layout->kernel_size,
  1970. smd, image_target);
  1971. if (!is_relocatable (image_target) &&
  1972. grub_host_to_target_addr (s->sh_addr) != image_target->link_addr)
  1973. {
  1974. char *msg
  1975. = grub_xasprintf (_("`%s' is miscompiled: its start address is 0x%llx"
  1976. " instead of 0x%llx: ld.gold bug?"),
  1977. kernel_path,
  1978. (unsigned long long) grub_host_to_target_addr (s->sh_addr),
  1979. (unsigned long long) image_target->link_addr);
  1980. grub_util_error ("%s", msg);
  1981. }
  1982. }
  1983. #ifdef MKIMAGE_ELF32
  1984. if (image_target->elf_target == EM_ARM)
  1985. {
  1986. grub_size_t tramp;
  1987. layout->kernel_size = ALIGN_UP (layout->kernel_size, 16);
  1988. tramp = arm_get_trampoline_size (e, smd->sections, smd->section_entsize,
  1989. smd->num_sections, image_target);
  1990. layout->tramp_off = layout->kernel_size;
  1991. layout->kernel_size += ALIGN_UP (tramp, 16);
  1992. }
  1993. #endif
  1994. layout->kernel_size = ALIGN_UP (layout->kernel_size + image_target->vaddr_offset,
  1995. image_target->section_align)
  1996. - image_target->vaddr_offset;
  1997. layout->exec_size = layout->kernel_size;
  1998. /* .data */
  1999. for (i = 0, s = smd->sections;
  2000. i < smd->num_sections;
  2001. i++, s = (Elf_Shdr *) ((char *) s + smd->section_entsize))
  2002. if (SUFFIX (is_data_section) (s, image_target))
  2003. layout->kernel_size = SUFFIX (put_section) (s, i, layout->kernel_size, smd,
  2004. image_target);
  2005. layout->bss_start = layout->kernel_size;
  2006. layout->end = layout->kernel_size;
  2007. /* .bss */
  2008. for (i = 0, s = smd->sections;
  2009. i < smd->num_sections;
  2010. i++, s = (Elf_Shdr *) ((char *) s + smd->section_entsize))
  2011. {
  2012. if (SUFFIX (is_bss_section) (s, image_target))
  2013. layout->end = SUFFIX (put_section) (s, i, layout->end, smd, image_target);
  2014. /*
  2015. * This must to be in the last time this function passes through the loop.
  2016. */
  2017. smd->vaddrs[i] = smd->addrs[i] + image_target->vaddr_offset;
  2018. }
  2019. layout->end = ALIGN_UP (layout->end + image_target->vaddr_offset,
  2020. image_target->section_align) - image_target->vaddr_offset;
  2021. /* Explicitly initialize BSS
  2022. when producing PE32 to avoid a bug in EFI implementations.
  2023. Platforms other than EFI and U-boot shouldn't have .bss in
  2024. their binaries as we build with -Wl,-Ttext.
  2025. */
  2026. if (image_target->id == IMAGE_EFI || !is_relocatable (image_target))
  2027. layout->kernel_size = layout->end;
  2028. }
  2029. char *
  2030. SUFFIX (grub_mkimage_load_image) (const char *kernel_path,
  2031. size_t total_module_size,
  2032. struct grub_mkimage_layout *layout,
  2033. const struct grub_install_image_target_desc *image_target)
  2034. {
  2035. char *kernel_img, *out_img;
  2036. struct section_metadata smd = { 0, 0, 0, 0, 0, 0, 0 };
  2037. Elf_Ehdr *e;
  2038. int i;
  2039. Elf_Shdr *s;
  2040. Elf_Off section_offset;
  2041. grub_size_t kernel_size;
  2042. grub_memset (layout, 0, sizeof (*layout));
  2043. layout->start_address = 0;
  2044. kernel_size = grub_util_get_image_size (kernel_path);
  2045. kernel_img = xmalloc (kernel_size);
  2046. grub_util_load_image (kernel_path, kernel_img);
  2047. e = (Elf_Ehdr *) kernel_img;
  2048. if (! SUFFIX (check_elf_header) (e, kernel_size, image_target))
  2049. grub_util_error ("invalid ELF header");
  2050. section_offset = grub_target_to_host (e->e_shoff);
  2051. smd.section_entsize = grub_target_to_host16 (e->e_shentsize);
  2052. smd.num_sections = grub_target_to_host16 (e->e_shnum);
  2053. if (kernel_size < section_offset
  2054. + (grub_uint32_t) smd.section_entsize * smd.num_sections)
  2055. grub_util_error (_("premature end of file %s"), kernel_path);
  2056. smd.sections = (Elf_Shdr *) (kernel_img + section_offset);
  2057. /* Relocate sections then symbols in the virtual address space. */
  2058. s = (Elf_Shdr *) ((char *) smd.sections
  2059. + grub_host_to_target16 (e->e_shstrndx) * smd.section_entsize);
  2060. smd.strtab = (char *) e + grub_host_to_target_addr (s->sh_offset);
  2061. smd.addrs = xcalloc (smd.num_sections, sizeof (*smd.addrs));
  2062. smd.vaddrs = xcalloc (smd.num_sections, sizeof (*smd.vaddrs));
  2063. SUFFIX (locate_sections) (e, kernel_path, &smd, layout, image_target);
  2064. if (!is_relocatable (image_target))
  2065. {
  2066. Elf_Addr current_address = layout->kernel_size;
  2067. for (i = 0, s = smd.sections;
  2068. i < smd.num_sections;
  2069. i++, s = (Elf_Shdr *) ((char *) s + smd.section_entsize))
  2070. if (grub_target_to_host32 (s->sh_type) == SHT_NOBITS)
  2071. {
  2072. Elf_Word sec_align = grub_host_to_target_addr (s->sh_addralign);
  2073. const char *name = smd.strtab + grub_host_to_target32 (s->sh_name);
  2074. if (sec_align)
  2075. current_address = ALIGN_UP (current_address
  2076. + image_target->vaddr_offset,
  2077. sec_align)
  2078. - image_target->vaddr_offset;
  2079. grub_util_info ("locating the section %s at 0x%"
  2080. GRUB_HOST_PRIxLONG_LONG,
  2081. name, (unsigned long long) current_address);
  2082. if (!is_relocatable (image_target))
  2083. current_address = grub_host_to_target_addr (s->sh_addr)
  2084. - image_target->link_addr;
  2085. smd.vaddrs[i] = current_address
  2086. + image_target->vaddr_offset;
  2087. current_address += grub_host_to_target_addr (s->sh_size);
  2088. }
  2089. current_address = ALIGN_UP (current_address + image_target->vaddr_offset,
  2090. image_target->section_align)
  2091. - image_target->vaddr_offset;
  2092. layout->bss_size = current_address - layout->kernel_size;
  2093. }
  2094. else
  2095. layout->bss_size = 0;
  2096. if (image_target->id == IMAGE_SPARC64_AOUT
  2097. || image_target->id == IMAGE_SPARC64_RAW
  2098. || image_target->id == IMAGE_UBOOT
  2099. || image_target->id == IMAGE_COREBOOT
  2100. || image_target->id == IMAGE_SPARC64_CDCORE)
  2101. layout->kernel_size = ALIGN_UP (layout->kernel_size, image_target->mod_align);
  2102. if (is_relocatable (image_target))
  2103. {
  2104. smd.symtab = NULL;
  2105. for (i = 0, s = smd.sections;
  2106. i < smd.num_sections;
  2107. i++, s = (Elf_Shdr *) ((char *) s + smd.section_entsize))
  2108. if (s->sh_type == grub_host_to_target32 (SHT_SYMTAB))
  2109. {
  2110. smd.symtab = s;
  2111. break;
  2112. }
  2113. if (! smd.symtab)
  2114. grub_util_error ("%s", _("no symbol table"));
  2115. #ifdef MKIMAGE_ELF64
  2116. if (image_target->elf_target == EM_IA_64)
  2117. {
  2118. grub_size_t tramp;
  2119. layout->kernel_size = ALIGN_UP (layout->kernel_size, 16);
  2120. grub_ia64_dl_get_tramp_got_size (e, &tramp, &layout->got_size);
  2121. layout->tramp_off = layout->kernel_size;
  2122. layout->kernel_size += ALIGN_UP (tramp, 16);
  2123. layout->ia64jmp_off = layout->kernel_size;
  2124. layout->ia64jmpnum = SUFFIX (count_funcs) (e, smd.symtab,
  2125. image_target);
  2126. layout->kernel_size += 16 * layout->ia64jmpnum;
  2127. layout->got_off = layout->kernel_size;
  2128. layout->kernel_size += ALIGN_UP (layout->got_size, 16);
  2129. }
  2130. if (image_target->elf_target == EM_AARCH64)
  2131. {
  2132. grub_size_t tramp;
  2133. layout->kernel_size = ALIGN_UP (layout->kernel_size, 16);
  2134. grub_arm64_dl_get_tramp_got_size (e, &tramp, &layout->got_size);
  2135. layout->got_off = layout->kernel_size;
  2136. layout->kernel_size += ALIGN_UP (layout->got_size, 16);
  2137. }
  2138. #endif
  2139. }
  2140. else
  2141. {
  2142. layout->reloc_size = 0;
  2143. layout->reloc_section = NULL;
  2144. }
  2145. out_img = xmalloc (layout->kernel_size + total_module_size);
  2146. memset (out_img, 0, layout->kernel_size + total_module_size);
  2147. if (is_relocatable (image_target))
  2148. {
  2149. layout->start_address = SUFFIX (relocate_symbols) (e, &smd,
  2150. (char *) out_img + layout->ia64jmp_off,
  2151. layout->ia64jmp_off + image_target->vaddr_offset,
  2152. layout->bss_start, layout->end, image_target);
  2153. if (layout->start_address == (Elf_Addr) -1)
  2154. grub_util_error ("start symbol is not defined");
  2155. /* Resolve addrs in the virtual address space. */
  2156. SUFFIX (relocate_addrs) (e, &smd, out_img, layout->tramp_off,
  2157. layout->got_off, image_target);
  2158. make_reloc_section (e, layout, &smd, image_target);
  2159. if (image_target->id != IMAGE_EFI)
  2160. {
  2161. out_img = xrealloc (out_img, layout->kernel_size + total_module_size
  2162. + ALIGN_UP (layout->reloc_size, image_target->mod_align));
  2163. memcpy (out_img + layout->kernel_size, layout->reloc_section, layout->reloc_size);
  2164. memset (out_img + layout->kernel_size + layout->reloc_size, 0,
  2165. total_module_size + ALIGN_UP (layout->reloc_size, image_target->mod_align) - layout->reloc_size);
  2166. layout->kernel_size += ALIGN_UP (layout->reloc_size, image_target->mod_align);
  2167. }
  2168. }
  2169. for (i = 0, s = smd.sections;
  2170. i < smd.num_sections;
  2171. i++, s = (Elf_Shdr *) ((char *) s + smd.section_entsize))
  2172. if (SUFFIX (is_kept_section) (s, image_target))
  2173. {
  2174. if (grub_target_to_host32 (s->sh_type) == SHT_NOBITS)
  2175. memset (out_img + smd.addrs[i], 0,
  2176. grub_host_to_target_addr (s->sh_size));
  2177. else
  2178. memcpy (out_img + smd.addrs[i],
  2179. kernel_img + grub_host_to_target_addr (s->sh_offset),
  2180. grub_host_to_target_addr (s->sh_size));
  2181. }
  2182. free (kernel_img);
  2183. free (smd.vaddrs);
  2184. smd.vaddrs = NULL;
  2185. free (smd.addrs);
  2186. smd.addrs = NULL;
  2187. return out_img;
  2188. }