elf-nacl.c 11 KB

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