elf-nacl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /* Native Client support for ELF
  2. Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "sysdep.h"
  15. #include "bfd.h"
  16. #include "libbfd.h"
  17. #include "elf-bfd.h"
  18. #include "elf-nacl.h"
  19. #include "elf/common.h"
  20. #include "elf/internal.h"
  21. static bool
  22. segment_executable (struct elf_segment_map *seg)
  23. {
  24. if (seg->p_flags_valid)
  25. return (seg->p_flags & PF_X) != 0;
  26. else
  27. {
  28. /* The p_flags value has not been computed yet,
  29. so we have to look through the sections. */
  30. unsigned int i;
  31. for (i = 0; i < seg->count; ++i)
  32. if (seg->sections[i]->flags & SEC_CODE)
  33. return true;
  34. }
  35. return false;
  36. }
  37. /* Determine if this segment is eligible to receive the file and program
  38. headers. It must be read-only and non-executable.
  39. Its first section must start far enough past the page boundary to
  40. allow space for the headers. */
  41. static bool
  42. segment_eligible_for_headers (struct elf_segment_map *seg,
  43. bfd_vma minpagesize, bfd_vma sizeof_headers)
  44. {
  45. unsigned int i;
  46. if (seg->count == 0 || seg->sections[0]->lma % minpagesize < sizeof_headers)
  47. return false;
  48. for (i = 0; i < seg->count; ++i)
  49. {
  50. if ((seg->sections[i]->flags & (SEC_CODE|SEC_READONLY)) != SEC_READONLY)
  51. return false;
  52. }
  53. return true;
  54. }
  55. /* We permute the segment_map to get BFD to do the file layout we want:
  56. The first non-executable PT_LOAD segment appears first in the file
  57. and contains the ELF file header and phdrs. */
  58. bool
  59. nacl_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
  60. {
  61. const struct elf_backend_data *const bed = get_elf_backend_data (abfd);
  62. struct elf_segment_map **m = &elf_seg_map (abfd);
  63. struct elf_segment_map **first_load = NULL;
  64. struct elf_segment_map **headers = NULL;
  65. int sizeof_headers;
  66. if (info != NULL && info->user_phdrs)
  67. /* The linker script used PHDRS explicitly, so don't change what the
  68. user asked for. */
  69. return true;
  70. if (info != NULL)
  71. /* We're doing linking, so evalute SIZEOF_HEADERS as in a linker script. */
  72. sizeof_headers = bfd_sizeof_headers (abfd, info);
  73. else
  74. {
  75. /* We're not doing linking, so this is objcopy or suchlike.
  76. We just need to collect the size of the existing headers. */
  77. struct elf_segment_map *seg;
  78. sizeof_headers = bed->s->sizeof_ehdr;
  79. for (seg = *m; seg != NULL; seg = seg->next)
  80. sizeof_headers += bed->s->sizeof_phdr;
  81. }
  82. while (*m != NULL)
  83. {
  84. struct elf_segment_map *seg = *m;
  85. if (seg->p_type == PT_LOAD)
  86. {
  87. bool executable = segment_executable (seg);
  88. if (executable
  89. && seg->count > 0
  90. && seg->sections[0]->vma % bed->minpagesize == 0)
  91. {
  92. asection *lastsec = seg->sections[seg->count - 1];
  93. bfd_vma end = lastsec->vma + lastsec->size;
  94. if (end % bed->minpagesize != 0)
  95. {
  96. /* This is an executable segment that starts on a page
  97. boundary but does not end on a page boundary. Fill
  98. it out to a whole page with code fill (the tail of
  99. the segment will not be within any section). Thus
  100. the entire code segment can be mapped from the file
  101. as whole pages and that mapping will contain only
  102. valid instructions.
  103. To accomplish this, we must fake out the code in
  104. assign_file_positions_for_load_sections (elf.c) so
  105. that it advances past the rest of the final page,
  106. rather than trying to put the next (unaligned, or
  107. unallocated) section. We do this by appending a
  108. dummy section record to this element in the segment
  109. map. No such output section ever actually exists,
  110. but this gets the layout logic to advance the file
  111. positions past this partial page. Since we are
  112. lying to BFD like this, nothing will ever know to
  113. write the section contents. So we do that by hand
  114. after the fact, in nacl_final_write_processing, below. */
  115. struct elf_segment_map *newseg;
  116. asection *sec;
  117. struct bfd_elf_section_data *secdata;
  118. BFD_ASSERT (!seg->p_size_valid);
  119. secdata = bfd_zalloc (abfd, sizeof *secdata);
  120. if (secdata == NULL)
  121. return false;
  122. sec = bfd_zalloc (abfd, sizeof *sec);
  123. if (sec == NULL)
  124. return false;
  125. /* Fill in only the fields that actually affect the logic
  126. in assign_file_positions_for_load_sections. */
  127. sec->vma = end;
  128. sec->lma = lastsec->lma + lastsec->size;
  129. sec->size = bed->minpagesize - (end % bed->minpagesize);
  130. sec->flags = (SEC_ALLOC | SEC_LOAD
  131. | SEC_READONLY | SEC_CODE | SEC_LINKER_CREATED);
  132. sec->used_by_bfd = secdata;
  133. secdata->this_hdr.sh_type = SHT_PROGBITS;
  134. secdata->this_hdr.sh_flags = SHF_ALLOC | SHF_EXECINSTR;
  135. secdata->this_hdr.sh_addr = sec->vma;
  136. secdata->this_hdr.sh_size = sec->size;
  137. newseg
  138. = bfd_alloc (abfd, (sizeof (*newseg)
  139. + seg->count * sizeof (asection *)));
  140. if (newseg == NULL)
  141. return false;
  142. memcpy (newseg, seg, (sizeof (*newseg) - sizeof (asection *)
  143. + seg->count * sizeof (asection *)));
  144. newseg->sections[newseg->count++] = sec;
  145. *m = seg = newseg;
  146. }
  147. }
  148. /* First, we're just finding the earliest PT_LOAD.
  149. By the normal rules, this will be the lowest-addressed one. */
  150. if (first_load == NULL)
  151. first_load = m;
  152. /* Now that we've noted the first PT_LOAD, we're looking for
  153. the first non-executable PT_LOAD with a nonempty p_filesz. */
  154. else if (headers == NULL
  155. && segment_eligible_for_headers (seg, bed->minpagesize,
  156. sizeof_headers))
  157. headers = m;
  158. }
  159. m = &seg->next;
  160. }
  161. if (headers != NULL)
  162. {
  163. struct elf_segment_map **last_load = NULL;
  164. struct elf_segment_map *seg;
  165. m = first_load;
  166. while ((seg = *m) != NULL)
  167. {
  168. if (seg->p_type == PT_LOAD)
  169. {
  170. /* Clear the flags on any previous segment that
  171. included the file header and phdrs. */
  172. seg->includes_filehdr = 0;
  173. seg->includes_phdrs = 0;
  174. seg->no_sort_lma = 1;
  175. /* Also strip out empty segments. */
  176. if (seg->count == 0)
  177. {
  178. if (headers == &seg->next)
  179. headers = m;
  180. *m = seg->next;
  181. continue;
  182. }
  183. last_load = m;
  184. }
  185. m = &seg->next;
  186. }
  187. /* This segment will include those headers instead. */
  188. seg = *headers;
  189. seg->includes_filehdr = 1;
  190. seg->includes_phdrs = 1;
  191. if (last_load != NULL && first_load != last_load && first_load != headers)
  192. {
  193. /* Put the first PT_LOAD header last. */
  194. struct elf_segment_map *first = *first_load;
  195. struct elf_segment_map *last = *last_load;
  196. *first_load = first->next;
  197. first->next = last->next;
  198. last->next = first;
  199. }
  200. }
  201. return true;
  202. }
  203. /* After nacl_modify_segment_map has done its work, the file layout has
  204. been done as we wanted. But the PT_LOAD phdrs are no longer in the
  205. proper order for the ELF rule that they must appear in ascending address
  206. order. So find the two segments we swapped before, and swap them back. */
  207. bool
  208. nacl_modify_headers (bfd *abfd, struct bfd_link_info *info)
  209. {
  210. if (info != NULL && info->user_phdrs)
  211. /* The linker script used PHDRS explicitly, so don't change what the
  212. user asked for. */
  213. ;
  214. else
  215. {
  216. struct elf_segment_map **m = &elf_seg_map (abfd);
  217. Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr;
  218. Elf_Internal_Phdr *p = phdr;
  219. /* Find the PT_LOAD that contains the headers (should be the first). */
  220. while (*m != NULL)
  221. {
  222. if ((*m)->p_type == PT_LOAD && (*m)->includes_filehdr)
  223. break;
  224. m = &(*m)->next;
  225. ++p;
  226. }
  227. if (*m != NULL)
  228. {
  229. struct elf_segment_map **first_load_seg = m;
  230. Elf_Internal_Phdr *first_load_phdr = p;
  231. struct elf_segment_map **next_load_seg = NULL;
  232. Elf_Internal_Phdr *next_load_phdr = NULL;
  233. /* Now move past that first one and find the PT_LOAD that should be
  234. before it by address order. */
  235. m = &(*m)->next;
  236. ++p;
  237. while (*m != NULL)
  238. {
  239. if (p->p_type == PT_LOAD && p->p_vaddr < first_load_phdr->p_vaddr)
  240. {
  241. next_load_seg = m;
  242. next_load_phdr = p;
  243. break;
  244. }
  245. m = &(*m)->next;
  246. ++p;
  247. }
  248. /* Swap their positions in the segment_map back to how they
  249. used to be. The phdrs have already been set up by now,
  250. so we have to slide up the earlier ones to insert the one
  251. that should be first. */
  252. if (next_load_seg != NULL)
  253. {
  254. Elf_Internal_Phdr move_phdr;
  255. struct elf_segment_map *first_seg = *first_load_seg;
  256. struct elf_segment_map *next_seg = *next_load_seg;
  257. struct elf_segment_map *first_next = first_seg->next;
  258. struct elf_segment_map *next_next = next_seg->next;
  259. if (next_load_seg == &first_seg->next)
  260. {
  261. *first_load_seg = next_seg;
  262. next_seg->next = first_seg;
  263. first_seg->next = next_next;
  264. }
  265. else
  266. {
  267. *first_load_seg = first_next;
  268. *next_load_seg = next_next;
  269. first_seg->next = *next_load_seg;
  270. *next_load_seg = first_seg;
  271. next_seg->next = *first_load_seg;
  272. *first_load_seg = next_seg;
  273. }
  274. move_phdr = *next_load_phdr;
  275. memmove (first_load_phdr + 1, first_load_phdr,
  276. (next_load_phdr - first_load_phdr) * sizeof move_phdr);
  277. *first_load_phdr = move_phdr;
  278. }
  279. }
  280. }
  281. return _bfd_elf_modify_headers (abfd, info);
  282. }
  283. bool
  284. nacl_final_write_processing (bfd *abfd)
  285. {
  286. struct elf_segment_map *seg;
  287. for (seg = elf_seg_map (abfd); seg != NULL; seg = seg->next)
  288. if (seg->p_type == PT_LOAD
  289. && seg->count > 1
  290. && seg->sections[seg->count - 1]->owner == NULL)
  291. {
  292. /* This is a fake section added in nacl_modify_segment_map, above.
  293. It's not a real BFD section, so nothing wrote its contents.
  294. Now write out its contents. */
  295. asection *sec = seg->sections[seg->count - 1];
  296. char *fill;
  297. BFD_ASSERT (sec->flags & SEC_LINKER_CREATED);
  298. BFD_ASSERT (sec->flags & SEC_CODE);
  299. BFD_ASSERT (sec->size > 0);
  300. fill = abfd->arch_info->fill (sec->size, bfd_big_endian (abfd), true);
  301. if (fill == NULL
  302. || bfd_seek (abfd, sec->filepos, SEEK_SET) != 0
  303. || bfd_bwrite (fill, sec->size, abfd) != sec->size)
  304. {
  305. /* We don't have a proper way to report an error here. So
  306. instead fudge things so that elf_write_shdrs_and_ehdr will
  307. fail. */
  308. elf_elfheader (abfd)->e_shoff = (file_ptr) -1;
  309. }
  310. free (fill);
  311. }
  312. return _bfd_elf_final_write_processing (abfd);
  313. }