elfcode.h 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997
  1. /* ELF executable support for BFD.
  2. Copyright (C) 1991-2022 Free Software Foundation, Inc.
  3. Written by Fred Fish @ Cygnus Support, from information published
  4. in "UNIX System V Release 4, Programmers Guide: ANSI C and
  5. Programming Support Tools". Sufficient support for gdb.
  6. Rewritten by Mark Eichin @ Cygnus Support, from information
  7. published in "System V Application Binary Interface", chapters 4
  8. and 5, as well as the various "Processor Supplement" documents
  9. derived from it. Added support for assembler and other object file
  10. utilities. Further work done by Ken Raeburn (Cygnus Support), Michael
  11. Meissner (Open Software Foundation), and Peter Hoogenboom (University
  12. of Utah) to finish and extend this.
  13. This file is part of BFD, the Binary File Descriptor library.
  14. This program is free software; you can redistribute it and/or modify
  15. it under the terms of the GNU General Public License as published by
  16. the Free Software Foundation; either version 3 of the License, or
  17. (at your option) any later version.
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22. You should have received a copy of the GNU General Public License
  23. along with this program; if not, write to the Free Software
  24. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  25. MA 02110-1301, USA. */
  26. /* Problems and other issues to resolve.
  27. (1) BFD expects there to be some fixed number of "sections" in
  28. the object file. I.E. there is a "section_count" variable in the
  29. bfd structure which contains the number of sections. However, ELF
  30. supports multiple "views" of a file. In particular, with current
  31. implementations, executable files typically have two tables, a
  32. program header table and a section header table, both of which
  33. partition the executable.
  34. In ELF-speak, the "linking view" of the file uses the section header
  35. table to access "sections" within the file, and the "execution view"
  36. uses the program header table to access "segments" within the file.
  37. "Segments" typically may contain all the data from one or more
  38. "sections".
  39. Note that the section header table is optional in ELF executables,
  40. but it is this information that is most useful to gdb. If the
  41. section header table is missing, then gdb should probably try
  42. to make do with the program header table. (FIXME)
  43. (2) The code in this file is compiled twice, once in 32-bit mode and
  44. once in 64-bit mode. More of it should be made size-independent
  45. and moved into elf.c.
  46. (3) ELF section symbols are handled rather sloppily now. This should
  47. be cleaned up, and ELF section symbols reconciled with BFD section
  48. symbols.
  49. (4) We need a published spec for 64-bit ELF. We've got some stuff here
  50. that we're using for SPARC V9 64-bit chips, but don't assume that
  51. it's cast in stone.
  52. */
  53. #include "sysdep.h"
  54. #include "bfd.h"
  55. #include "libiberty.h"
  56. #include "bfdlink.h"
  57. #include "libbfd.h"
  58. #include "elf-bfd.h"
  59. #include "libiberty.h"
  60. /* Renaming structures, typedefs, macros and functions to be size-specific. */
  61. #define Elf_External_Ehdr NAME(Elf,External_Ehdr)
  62. #define Elf_External_Sym NAME(Elf,External_Sym)
  63. #define Elf_External_Shdr NAME(Elf,External_Shdr)
  64. #define Elf_External_Phdr NAME(Elf,External_Phdr)
  65. #define Elf_External_Rel NAME(Elf,External_Rel)
  66. #define Elf_External_Rela NAME(Elf,External_Rela)
  67. #define Elf_External_Dyn NAME(Elf,External_Dyn)
  68. #define elf_core_file_failing_command NAME(bfd_elf,core_file_failing_command)
  69. #define elf_core_file_failing_signal NAME(bfd_elf,core_file_failing_signal)
  70. #define elf_core_file_matches_executable_p \
  71. NAME(bfd_elf,core_file_matches_executable_p)
  72. #define elf_core_file_pid NAME(bfd_elf,core_file_pid)
  73. #define elf_object_p NAME(bfd_elf,object_p)
  74. #define elf_core_file_p NAME(bfd_elf,core_file_p)
  75. #define elf_get_symtab_upper_bound NAME(bfd_elf,get_symtab_upper_bound)
  76. #define elf_get_dynamic_symtab_upper_bound \
  77. NAME(bfd_elf,get_dynamic_symtab_upper_bound)
  78. #define elf_swap_reloc_in NAME(bfd_elf,swap_reloc_in)
  79. #define elf_swap_reloca_in NAME(bfd_elf,swap_reloca_in)
  80. #define elf_swap_reloc_out NAME(bfd_elf,swap_reloc_out)
  81. #define elf_swap_reloca_out NAME(bfd_elf,swap_reloca_out)
  82. #define elf_swap_symbol_in NAME(bfd_elf,swap_symbol_in)
  83. #define elf_swap_symbol_out NAME(bfd_elf,swap_symbol_out)
  84. #define elf_swap_phdr_in NAME(bfd_elf,swap_phdr_in)
  85. #define elf_swap_phdr_out NAME(bfd_elf,swap_phdr_out)
  86. #define elf_swap_dyn_in NAME(bfd_elf,swap_dyn_in)
  87. #define elf_swap_dyn_out NAME(bfd_elf,swap_dyn_out)
  88. #define elf_get_reloc_upper_bound NAME(bfd_elf,get_reloc_upper_bound)
  89. #define elf_canonicalize_reloc NAME(bfd_elf,canonicalize_reloc)
  90. #define elf_slurp_symbol_table NAME(bfd_elf,slurp_symbol_table)
  91. #define elf_canonicalize_symtab NAME(bfd_elf,canonicalize_symtab)
  92. #define elf_canonicalize_dynamic_symtab \
  93. NAME(bfd_elf,canonicalize_dynamic_symtab)
  94. #define elf_get_synthetic_symtab \
  95. NAME(bfd_elf,get_synthetic_symtab)
  96. #define elf_make_empty_symbol NAME(bfd_elf,make_empty_symbol)
  97. #define elf_get_symbol_info NAME(bfd_elf,get_symbol_info)
  98. #define elf_get_lineno NAME(bfd_elf,get_lineno)
  99. #define elf_set_arch_mach NAME(bfd_elf,set_arch_mach)
  100. #define elf_find_nearest_line NAME(bfd_elf,find_nearest_line)
  101. #define elf_sizeof_headers NAME(bfd_elf,sizeof_headers)
  102. #define elf_set_section_contents NAME(bfd_elf,set_section_contents)
  103. #define elf_no_info_to_howto NAME(bfd_elf,no_info_to_howto)
  104. #define elf_no_info_to_howto_rel NAME(bfd_elf,no_info_to_howto_rel)
  105. #define elf_find_section NAME(bfd_elf,find_section)
  106. #define elf_write_shdrs_and_ehdr NAME(bfd_elf,write_shdrs_and_ehdr)
  107. #define elf_write_out_phdrs NAME(bfd_elf,write_out_phdrs)
  108. #define elf_checksum_contents NAME(bfd_elf,checksum_contents)
  109. #define elf_write_relocs NAME(bfd_elf,write_relocs)
  110. #define elf_slurp_reloc_table NAME(bfd_elf,slurp_reloc_table)
  111. #if ARCH_SIZE == 64
  112. #define ELF_R_INFO(X,Y) ELF64_R_INFO(X,Y)
  113. #define ELF_R_SYM(X) ELF64_R_SYM(X)
  114. #define ELF_R_TYPE(X) ELF64_R_TYPE(X)
  115. #define ELFCLASS ELFCLASS64
  116. #define FILE_ALIGN 8
  117. #define LOG_FILE_ALIGN 3
  118. #endif
  119. #if ARCH_SIZE == 32
  120. #define ELF_R_INFO(X,Y) ELF32_R_INFO(X,Y)
  121. #define ELF_R_SYM(X) ELF32_R_SYM(X)
  122. #define ELF_R_TYPE(X) ELF32_R_TYPE(X)
  123. #define ELFCLASS ELFCLASS32
  124. #define FILE_ALIGN 4
  125. #define LOG_FILE_ALIGN 2
  126. #endif
  127. #if DEBUG & 2
  128. static void elf_debug_section (int, Elf_Internal_Shdr *);
  129. #endif
  130. #if DEBUG & 1
  131. static void elf_debug_file (Elf_Internal_Ehdr *);
  132. #endif
  133. /* Structure swapping routines */
  134. /* Should perhaps use put_offset, put_word, etc. For now, the two versions
  135. can be handled by explicitly specifying 32 bits or "the long type". */
  136. #if ARCH_SIZE == 64
  137. #define H_PUT_WORD H_PUT_64
  138. #define H_PUT_SIGNED_WORD H_PUT_S64
  139. #define H_GET_WORD H_GET_64
  140. #define H_GET_SIGNED_WORD H_GET_S64
  141. #endif
  142. #if ARCH_SIZE == 32
  143. #define H_PUT_WORD H_PUT_32
  144. #define H_PUT_SIGNED_WORD H_PUT_S32
  145. #define H_GET_WORD H_GET_32
  146. #define H_GET_SIGNED_WORD H_GET_S32
  147. #endif
  148. /* Translate an ELF symbol in external format into an ELF symbol in internal
  149. format. */
  150. bool
  151. elf_swap_symbol_in (bfd *abfd,
  152. const void *psrc,
  153. const void *pshn,
  154. Elf_Internal_Sym *dst)
  155. {
  156. const Elf_External_Sym *src = (const Elf_External_Sym *) psrc;
  157. const Elf_External_Sym_Shndx *shndx = (const Elf_External_Sym_Shndx *) pshn;
  158. int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
  159. dst->st_name = H_GET_32 (abfd, src->st_name);
  160. if (signed_vma)
  161. dst->st_value = H_GET_SIGNED_WORD (abfd, src->st_value);
  162. else
  163. dst->st_value = H_GET_WORD (abfd, src->st_value);
  164. dst->st_size = H_GET_WORD (abfd, src->st_size);
  165. dst->st_info = H_GET_8 (abfd, src->st_info);
  166. dst->st_other = H_GET_8 (abfd, src->st_other);
  167. dst->st_shndx = H_GET_16 (abfd, src->st_shndx);
  168. if (dst->st_shndx == (SHN_XINDEX & 0xffff))
  169. {
  170. if (shndx == NULL)
  171. return false;
  172. dst->st_shndx = H_GET_32 (abfd, shndx->est_shndx);
  173. }
  174. else if (dst->st_shndx >= (SHN_LORESERVE & 0xffff))
  175. dst->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
  176. dst->st_target_internal = 0;
  177. return true;
  178. }
  179. /* Translate an ELF symbol in internal format into an ELF symbol in external
  180. format. */
  181. void
  182. elf_swap_symbol_out (bfd *abfd,
  183. const Elf_Internal_Sym *src,
  184. void *cdst,
  185. void *shndx)
  186. {
  187. unsigned int tmp;
  188. Elf_External_Sym *dst = (Elf_External_Sym *) cdst;
  189. H_PUT_32 (abfd, src->st_name, dst->st_name);
  190. H_PUT_WORD (abfd, src->st_value, dst->st_value);
  191. H_PUT_WORD (abfd, src->st_size, dst->st_size);
  192. H_PUT_8 (abfd, src->st_info, dst->st_info);
  193. H_PUT_8 (abfd, src->st_other, dst->st_other);
  194. tmp = src->st_shndx;
  195. if (tmp >= (SHN_LORESERVE & 0xffff) && tmp < SHN_LORESERVE)
  196. {
  197. if (shndx == NULL)
  198. abort ();
  199. H_PUT_32 (abfd, tmp, shndx);
  200. tmp = SHN_XINDEX & 0xffff;
  201. }
  202. H_PUT_16 (abfd, tmp, dst->st_shndx);
  203. }
  204. /* Translate an ELF file header in external format into an ELF file header in
  205. internal format. */
  206. static void
  207. elf_swap_ehdr_in (bfd *abfd,
  208. const Elf_External_Ehdr *src,
  209. Elf_Internal_Ehdr *dst)
  210. {
  211. int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
  212. memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
  213. dst->e_type = H_GET_16 (abfd, src->e_type);
  214. dst->e_machine = H_GET_16 (abfd, src->e_machine);
  215. dst->e_version = H_GET_32 (abfd, src->e_version);
  216. if (signed_vma)
  217. dst->e_entry = H_GET_SIGNED_WORD (abfd, src->e_entry);
  218. else
  219. dst->e_entry = H_GET_WORD (abfd, src->e_entry);
  220. dst->e_phoff = H_GET_WORD (abfd, src->e_phoff);
  221. dst->e_shoff = H_GET_WORD (abfd, src->e_shoff);
  222. dst->e_flags = H_GET_32 (abfd, src->e_flags);
  223. dst->e_ehsize = H_GET_16 (abfd, src->e_ehsize);
  224. dst->e_phentsize = H_GET_16 (abfd, src->e_phentsize);
  225. dst->e_phnum = H_GET_16 (abfd, src->e_phnum);
  226. dst->e_shentsize = H_GET_16 (abfd, src->e_shentsize);
  227. dst->e_shnum = H_GET_16 (abfd, src->e_shnum);
  228. dst->e_shstrndx = H_GET_16 (abfd, src->e_shstrndx);
  229. }
  230. /* Translate an ELF file header in internal format into an ELF file header in
  231. external format. */
  232. static void
  233. elf_swap_ehdr_out (bfd *abfd,
  234. const Elf_Internal_Ehdr *src,
  235. Elf_External_Ehdr *dst)
  236. {
  237. unsigned int tmp;
  238. int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
  239. memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
  240. /* note that all elements of dst are *arrays of unsigned char* already... */
  241. H_PUT_16 (abfd, src->e_type, dst->e_type);
  242. H_PUT_16 (abfd, src->e_machine, dst->e_machine);
  243. H_PUT_32 (abfd, src->e_version, dst->e_version);
  244. if (signed_vma)
  245. H_PUT_SIGNED_WORD (abfd, src->e_entry, dst->e_entry);
  246. else
  247. H_PUT_WORD (abfd, src->e_entry, dst->e_entry);
  248. H_PUT_WORD (abfd, src->e_phoff, dst->e_phoff);
  249. H_PUT_WORD (abfd, src->e_shoff, dst->e_shoff);
  250. H_PUT_32 (abfd, src->e_flags, dst->e_flags);
  251. H_PUT_16 (abfd, src->e_ehsize, dst->e_ehsize);
  252. H_PUT_16 (abfd, src->e_phentsize, dst->e_phentsize);
  253. tmp = src->e_phnum;
  254. if (tmp > PN_XNUM)
  255. tmp = PN_XNUM;
  256. H_PUT_16 (abfd, tmp, dst->e_phnum);
  257. H_PUT_16 (abfd, src->e_shentsize, dst->e_shentsize);
  258. tmp = src->e_shnum;
  259. if (tmp >= (SHN_LORESERVE & 0xffff))
  260. tmp = SHN_UNDEF;
  261. H_PUT_16 (abfd, tmp, dst->e_shnum);
  262. tmp = src->e_shstrndx;
  263. if (tmp >= (SHN_LORESERVE & 0xffff))
  264. tmp = SHN_XINDEX & 0xffff;
  265. H_PUT_16 (abfd, tmp, dst->e_shstrndx);
  266. }
  267. /* Translate an ELF section header table entry in external format into an
  268. ELF section header table entry in internal format. */
  269. static void
  270. elf_swap_shdr_in (bfd *abfd,
  271. const Elf_External_Shdr *src,
  272. Elf_Internal_Shdr *dst)
  273. {
  274. int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
  275. dst->sh_name = H_GET_32 (abfd, src->sh_name);
  276. dst->sh_type = H_GET_32 (abfd, src->sh_type);
  277. dst->sh_flags = H_GET_WORD (abfd, src->sh_flags);
  278. if (signed_vma)
  279. dst->sh_addr = H_GET_SIGNED_WORD (abfd, src->sh_addr);
  280. else
  281. dst->sh_addr = H_GET_WORD (abfd, src->sh_addr);
  282. dst->sh_offset = H_GET_WORD (abfd, src->sh_offset);
  283. dst->sh_size = H_GET_WORD (abfd, src->sh_size);
  284. /* PR 23657. Check for invalid section size, in sections with contents.
  285. Note - we do not set an error value here because the contents
  286. of this particular section might not be needed by the consumer. */
  287. if (dst->sh_type != SHT_NOBITS)
  288. {
  289. ufile_ptr filesize = bfd_get_file_size (abfd);
  290. if (filesize != 0
  291. && ((ufile_ptr) dst->sh_offset > filesize
  292. || dst->sh_size > filesize - dst->sh_offset))
  293. {
  294. if (!abfd->read_only)
  295. _bfd_error_handler (_("warning: %pB has a section "
  296. "extending past end of file"), abfd);
  297. abfd->read_only = 1;
  298. }
  299. }
  300. dst->sh_link = H_GET_32 (abfd, src->sh_link);
  301. dst->sh_info = H_GET_32 (abfd, src->sh_info);
  302. dst->sh_addralign = H_GET_WORD (abfd, src->sh_addralign);
  303. dst->sh_entsize = H_GET_WORD (abfd, src->sh_entsize);
  304. dst->bfd_section = NULL;
  305. dst->contents = NULL;
  306. }
  307. /* Translate an ELF section header table entry in internal format into an
  308. ELF section header table entry in external format. */
  309. static void
  310. elf_swap_shdr_out (bfd *abfd,
  311. const Elf_Internal_Shdr *src,
  312. Elf_External_Shdr *dst)
  313. {
  314. /* note that all elements of dst are *arrays of unsigned char* already... */
  315. H_PUT_32 (abfd, src->sh_name, dst->sh_name);
  316. H_PUT_32 (abfd, src->sh_type, dst->sh_type);
  317. H_PUT_WORD (abfd, src->sh_flags, dst->sh_flags);
  318. H_PUT_WORD (abfd, src->sh_addr, dst->sh_addr);
  319. H_PUT_WORD (abfd, src->sh_offset, dst->sh_offset);
  320. H_PUT_WORD (abfd, src->sh_size, dst->sh_size);
  321. H_PUT_32 (abfd, src->sh_link, dst->sh_link);
  322. H_PUT_32 (abfd, src->sh_info, dst->sh_info);
  323. H_PUT_WORD (abfd, src->sh_addralign, dst->sh_addralign);
  324. H_PUT_WORD (abfd, src->sh_entsize, dst->sh_entsize);
  325. }
  326. /* Translate an ELF program header table entry in external format into an
  327. ELF program header table entry in internal format. */
  328. void
  329. elf_swap_phdr_in (bfd *abfd,
  330. const Elf_External_Phdr *src,
  331. Elf_Internal_Phdr *dst)
  332. {
  333. int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
  334. dst->p_type = H_GET_32 (abfd, src->p_type);
  335. dst->p_flags = H_GET_32 (abfd, src->p_flags);
  336. dst->p_offset = H_GET_WORD (abfd, src->p_offset);
  337. if (signed_vma)
  338. {
  339. dst->p_vaddr = H_GET_SIGNED_WORD (abfd, src->p_vaddr);
  340. dst->p_paddr = H_GET_SIGNED_WORD (abfd, src->p_paddr);
  341. }
  342. else
  343. {
  344. dst->p_vaddr = H_GET_WORD (abfd, src->p_vaddr);
  345. dst->p_paddr = H_GET_WORD (abfd, src->p_paddr);
  346. }
  347. dst->p_filesz = H_GET_WORD (abfd, src->p_filesz);
  348. dst->p_memsz = H_GET_WORD (abfd, src->p_memsz);
  349. dst->p_align = H_GET_WORD (abfd, src->p_align);
  350. }
  351. void
  352. elf_swap_phdr_out (bfd *abfd,
  353. const Elf_Internal_Phdr *src,
  354. Elf_External_Phdr *dst)
  355. {
  356. const struct elf_backend_data *bed;
  357. bfd_vma p_paddr;
  358. bed = get_elf_backend_data (abfd);
  359. p_paddr = bed->want_p_paddr_set_to_zero ? 0 : src->p_paddr;
  360. /* note that all elements of dst are *arrays of unsigned char* already... */
  361. H_PUT_32 (abfd, src->p_type, dst->p_type);
  362. H_PUT_WORD (abfd, src->p_offset, dst->p_offset);
  363. H_PUT_WORD (abfd, src->p_vaddr, dst->p_vaddr);
  364. H_PUT_WORD (abfd, p_paddr, dst->p_paddr);
  365. H_PUT_WORD (abfd, src->p_filesz, dst->p_filesz);
  366. H_PUT_WORD (abfd, src->p_memsz, dst->p_memsz);
  367. H_PUT_32 (abfd, src->p_flags, dst->p_flags);
  368. H_PUT_WORD (abfd, src->p_align, dst->p_align);
  369. }
  370. /* Translate an ELF reloc from external format to internal format. */
  371. void
  372. elf_swap_reloc_in (bfd *abfd,
  373. const bfd_byte *s,
  374. Elf_Internal_Rela *dst)
  375. {
  376. const Elf_External_Rel *src = (const Elf_External_Rel *) s;
  377. dst->r_offset = H_GET_WORD (abfd, src->r_offset);
  378. dst->r_info = H_GET_WORD (abfd, src->r_info);
  379. dst->r_addend = 0;
  380. }
  381. void
  382. elf_swap_reloca_in (bfd *abfd,
  383. const bfd_byte *s,
  384. Elf_Internal_Rela *dst)
  385. {
  386. const Elf_External_Rela *src = (const Elf_External_Rela *) s;
  387. dst->r_offset = H_GET_WORD (abfd, src->r_offset);
  388. dst->r_info = H_GET_WORD (abfd, src->r_info);
  389. dst->r_addend = H_GET_SIGNED_WORD (abfd, src->r_addend);
  390. }
  391. /* Translate an ELF reloc from internal format to external format. */
  392. void
  393. elf_swap_reloc_out (bfd *abfd,
  394. const Elf_Internal_Rela *src,
  395. bfd_byte *d)
  396. {
  397. Elf_External_Rel *dst = (Elf_External_Rel *) d;
  398. H_PUT_WORD (abfd, src->r_offset, dst->r_offset);
  399. H_PUT_WORD (abfd, src->r_info, dst->r_info);
  400. }
  401. void
  402. elf_swap_reloca_out (bfd *abfd,
  403. const Elf_Internal_Rela *src,
  404. bfd_byte *d)
  405. {
  406. Elf_External_Rela *dst = (Elf_External_Rela *) d;
  407. H_PUT_WORD (abfd, src->r_offset, dst->r_offset);
  408. H_PUT_WORD (abfd, src->r_info, dst->r_info);
  409. H_PUT_SIGNED_WORD (abfd, src->r_addend, dst->r_addend);
  410. }
  411. void
  412. elf_swap_dyn_in (bfd *abfd,
  413. const void *p,
  414. Elf_Internal_Dyn *dst)
  415. {
  416. const Elf_External_Dyn *src = (const Elf_External_Dyn *) p;
  417. dst->d_tag = H_GET_WORD (abfd, src->d_tag);
  418. dst->d_un.d_val = H_GET_WORD (abfd, src->d_un.d_val);
  419. }
  420. void
  421. elf_swap_dyn_out (bfd *abfd,
  422. const Elf_Internal_Dyn *src,
  423. void *p)
  424. {
  425. Elf_External_Dyn *dst = (Elf_External_Dyn *) p;
  426. H_PUT_WORD (abfd, src->d_tag, dst->d_tag);
  427. H_PUT_WORD (abfd, src->d_un.d_val, dst->d_un.d_val);
  428. }
  429. /* ELF .o/exec file reading */
  430. /* Begin processing a given object.
  431. First we validate the file by reading in the ELF header and checking
  432. the magic number. */
  433. static inline bool
  434. elf_file_p (Elf_External_Ehdr *x_ehdrp)
  435. {
  436. return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0)
  437. && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1)
  438. && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2)
  439. && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3));
  440. }
  441. /* Check to see if the file associated with ABFD matches the target vector
  442. that ABFD points to.
  443. Note that we may be called several times with the same ABFD, but different
  444. target vectors, most of which will not match. We have to avoid leaving
  445. any side effects in ABFD, or any data it points to (like tdata), if the
  446. file does not match the target vector. */
  447. bfd_cleanup
  448. elf_object_p (bfd *abfd)
  449. {
  450. Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
  451. Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
  452. Elf_External_Shdr x_shdr; /* Section header table entry, external form */
  453. Elf_Internal_Shdr i_shdr;
  454. Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */
  455. unsigned int shindex;
  456. const struct elf_backend_data *ebd;
  457. asection *s;
  458. const bfd_target *target;
  459. /* Read in the ELF header in external format. */
  460. if (bfd_bread (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
  461. {
  462. if (bfd_get_error () != bfd_error_system_call)
  463. goto got_wrong_format_error;
  464. else
  465. goto got_no_match;
  466. }
  467. /* Now check to see if we have a valid ELF file, and one that BFD can
  468. make use of. The magic number must match, the address size ('class')
  469. and byte-swapping must match our XVEC entry, and it must have a
  470. section header table (FIXME: See comments re sections at top of this
  471. file). */
  472. if (! elf_file_p (&x_ehdr)
  473. || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
  474. || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
  475. goto got_wrong_format_error;
  476. /* Check that file's byte order matches xvec's */
  477. switch (x_ehdr.e_ident[EI_DATA])
  478. {
  479. case ELFDATA2MSB: /* Big-endian */
  480. if (! bfd_header_big_endian (abfd))
  481. goto got_wrong_format_error;
  482. break;
  483. case ELFDATA2LSB: /* Little-endian */
  484. if (! bfd_header_little_endian (abfd))
  485. goto got_wrong_format_error;
  486. break;
  487. case ELFDATANONE: /* No data encoding specified */
  488. default: /* Unknown data encoding specified */
  489. goto got_wrong_format_error;
  490. }
  491. target = abfd->xvec;
  492. /* Allocate an instance of the elf_obj_tdata structure and hook it up to
  493. the tdata pointer in the bfd. */
  494. if (! (*target->_bfd_set_format[bfd_object]) (abfd))
  495. goto got_no_match;
  496. /* Now that we know the byte order, swap in the rest of the header */
  497. i_ehdrp = elf_elfheader (abfd);
  498. elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
  499. #if DEBUG & 1
  500. elf_debug_file (i_ehdrp);
  501. #endif
  502. /* Reject ET_CORE (header indicates core file, not object file) */
  503. if (i_ehdrp->e_type == ET_CORE)
  504. goto got_wrong_format_error;
  505. /* If this is a relocatable file and there is no section header
  506. table, then we're hosed. */
  507. if (i_ehdrp->e_shoff < sizeof (x_ehdr) && i_ehdrp->e_type == ET_REL)
  508. goto got_wrong_format_error;
  509. /* As a simple sanity check, verify that what BFD thinks is the
  510. size of each section header table entry actually matches the size
  511. recorded in the file, but only if there are any sections. */
  512. if (i_ehdrp->e_shentsize != sizeof (x_shdr) && i_ehdrp->e_shnum != 0)
  513. goto got_wrong_format_error;
  514. /* Further sanity check. */
  515. if (i_ehdrp->e_shoff < sizeof (x_ehdr) && i_ehdrp->e_shnum != 0)
  516. goto got_wrong_format_error;
  517. ebd = get_elf_backend_data (abfd);
  518. if (ebd->s->arch_size != ARCH_SIZE)
  519. goto got_wrong_format_error;
  520. /* Check that the ELF e_machine field matches what this particular
  521. BFD format expects. */
  522. if (ebd->elf_machine_code != i_ehdrp->e_machine
  523. && (ebd->elf_machine_alt1 == 0
  524. || i_ehdrp->e_machine != ebd->elf_machine_alt1)
  525. && (ebd->elf_machine_alt2 == 0
  526. || i_ehdrp->e_machine != ebd->elf_machine_alt2)
  527. && ebd->elf_machine_code != EM_NONE)
  528. goto got_wrong_format_error;
  529. if (i_ehdrp->e_type == ET_EXEC)
  530. abfd->flags |= EXEC_P;
  531. else if (i_ehdrp->e_type == ET_DYN)
  532. abfd->flags |= DYNAMIC;
  533. if (i_ehdrp->e_phnum > 0)
  534. abfd->flags |= D_PAGED;
  535. if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0))
  536. {
  537. /* It's OK if this fails for the generic target. */
  538. if (ebd->elf_machine_code != EM_NONE)
  539. goto got_no_match;
  540. }
  541. if (ebd->elf_machine_code != EM_NONE
  542. && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
  543. && ebd->elf_osabi != ELFOSABI_NONE)
  544. goto got_wrong_format_error;
  545. if (i_ehdrp->e_shoff >= sizeof (x_ehdr))
  546. {
  547. file_ptr where = (file_ptr) i_ehdrp->e_shoff;
  548. /* Seek to the section header table in the file. */
  549. if (bfd_seek (abfd, where, SEEK_SET) != 0)
  550. goto got_no_match;
  551. /* Read the first section header at index 0, and convert to internal
  552. form. */
  553. if (bfd_bread (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
  554. goto got_no_match;
  555. elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
  556. /* If the section count is zero, the actual count is in the first
  557. section header. */
  558. if (i_ehdrp->e_shnum == SHN_UNDEF)
  559. {
  560. i_ehdrp->e_shnum = i_shdr.sh_size;
  561. if (i_ehdrp->e_shnum >= SHN_LORESERVE
  562. || i_ehdrp->e_shnum != i_shdr.sh_size
  563. || i_ehdrp->e_shnum == 0)
  564. goto got_wrong_format_error;
  565. }
  566. /* And similarly for the string table index. */
  567. if (i_ehdrp->e_shstrndx == (SHN_XINDEX & 0xffff))
  568. {
  569. i_ehdrp->e_shstrndx = i_shdr.sh_link;
  570. if (i_ehdrp->e_shstrndx != i_shdr.sh_link)
  571. goto got_wrong_format_error;
  572. }
  573. /* And program headers. */
  574. if (i_ehdrp->e_phnum == PN_XNUM && i_shdr.sh_info != 0)
  575. {
  576. i_ehdrp->e_phnum = i_shdr.sh_info;
  577. if (i_ehdrp->e_phnum != i_shdr.sh_info)
  578. goto got_wrong_format_error;
  579. }
  580. /* Sanity check that we can read all of the section headers.
  581. It ought to be good enough to just read the last one. */
  582. if (i_ehdrp->e_shnum != 1)
  583. {
  584. /* Check that we don't have a totally silly number of sections. */
  585. if (i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (x_shdr)
  586. || i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (i_shdr))
  587. goto got_wrong_format_error;
  588. where += (i_ehdrp->e_shnum - 1) * sizeof (x_shdr);
  589. if ((bfd_size_type) where <= i_ehdrp->e_shoff)
  590. goto got_wrong_format_error;
  591. if (bfd_seek (abfd, where, SEEK_SET) != 0)
  592. goto got_no_match;
  593. if (bfd_bread (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
  594. goto got_no_match;
  595. /* Back to where we were. */
  596. where = i_ehdrp->e_shoff + sizeof (x_shdr);
  597. if (bfd_seek (abfd, where, SEEK_SET) != 0)
  598. goto got_no_match;
  599. }
  600. }
  601. /* Allocate space for a copy of the section header table in
  602. internal form. */
  603. if (i_ehdrp->e_shnum != 0)
  604. {
  605. Elf_Internal_Shdr *shdrp;
  606. unsigned int num_sec;
  607. size_t amt;
  608. if (_bfd_mul_overflow (i_ehdrp->e_shnum, sizeof (*i_shdrp), &amt))
  609. goto got_wrong_format_error;
  610. i_shdrp = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
  611. if (!i_shdrp)
  612. goto got_no_match;
  613. num_sec = i_ehdrp->e_shnum;
  614. elf_numsections (abfd) = num_sec;
  615. if (_bfd_mul_overflow (num_sec, sizeof (i_shdrp), &amt))
  616. goto got_wrong_format_error;
  617. elf_elfsections (abfd) = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
  618. if (!elf_elfsections (abfd))
  619. goto got_no_match;
  620. elf_tdata (abfd)->being_created = bfd_zalloc (abfd, num_sec);
  621. if (!elf_tdata (abfd)->being_created)
  622. goto got_no_match;
  623. memcpy (i_shdrp, &i_shdr, sizeof (*i_shdrp));
  624. for (shdrp = i_shdrp, shindex = 0; shindex < num_sec; shindex++)
  625. elf_elfsections (abfd)[shindex] = shdrp++;
  626. /* Read in the rest of the section header table and convert it
  627. to internal form. */
  628. for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
  629. {
  630. if (bfd_bread (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
  631. goto got_no_match;
  632. elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
  633. /* Sanity check sh_link and sh_info. */
  634. if (i_shdrp[shindex].sh_link >= num_sec)
  635. {
  636. /* PR 10478: Accept Solaris binaries with a sh_link
  637. field set to SHN_BEFORE or SHN_AFTER. */
  638. switch (ebd->elf_machine_code)
  639. {
  640. case EM_386:
  641. case EM_IAMCU:
  642. case EM_X86_64:
  643. case EM_OLD_SPARCV9:
  644. case EM_SPARC32PLUS:
  645. case EM_SPARCV9:
  646. case EM_SPARC:
  647. if (i_shdrp[shindex].sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
  648. || i_shdrp[shindex].sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
  649. break;
  650. /* Otherwise fall through. */
  651. default:
  652. goto got_wrong_format_error;
  653. }
  654. }
  655. if (((i_shdrp[shindex].sh_flags & SHF_INFO_LINK)
  656. || i_shdrp[shindex].sh_type == SHT_RELA
  657. || i_shdrp[shindex].sh_type == SHT_REL)
  658. && i_shdrp[shindex].sh_info >= num_sec)
  659. goto got_wrong_format_error;
  660. /* If the section is loaded, but not page aligned, clear
  661. D_PAGED. */
  662. if (i_shdrp[shindex].sh_size != 0
  663. && (i_shdrp[shindex].sh_flags & SHF_ALLOC) != 0
  664. && i_shdrp[shindex].sh_type != SHT_NOBITS
  665. && (((i_shdrp[shindex].sh_addr - i_shdrp[shindex].sh_offset)
  666. % ebd->minpagesize)
  667. != 0))
  668. abfd->flags &= ~D_PAGED;
  669. }
  670. if (i_ehdrp->e_shstrndx >= elf_numsections (abfd)
  671. || i_shdrp[i_ehdrp->e_shstrndx].sh_type != SHT_STRTAB)
  672. {
  673. /* PR 2257:
  674. We used to just goto got_wrong_format_error here
  675. but there are binaries in existance for which this test
  676. will prevent the binutils from working with them at all.
  677. So we are kind, and reset the string index value to 0
  678. so that at least some processing can be done. */
  679. i_ehdrp->e_shstrndx = SHN_UNDEF;
  680. abfd->read_only = 1;
  681. _bfd_error_handler
  682. (_("warning: %pB has a corrupt string table index - ignoring"),
  683. abfd);
  684. }
  685. }
  686. else if (i_ehdrp->e_shstrndx != SHN_UNDEF)
  687. goto got_wrong_format_error;
  688. /* Read in the program headers. */
  689. if (i_ehdrp->e_phnum == 0)
  690. elf_tdata (abfd)->phdr = NULL;
  691. else
  692. {
  693. Elf_Internal_Phdr *i_phdr;
  694. unsigned int i;
  695. ufile_ptr filesize;
  696. size_t amt;
  697. /* Check for a corrupt input file with an impossibly large number
  698. of program headers. */
  699. filesize = bfd_get_file_size (abfd);
  700. if (filesize != 0
  701. && i_ehdrp->e_phnum > filesize / sizeof (Elf_External_Phdr))
  702. goto got_wrong_format_error;
  703. if (_bfd_mul_overflow (i_ehdrp->e_phnum, sizeof (*i_phdr), &amt))
  704. goto got_wrong_format_error;
  705. elf_tdata (abfd)->phdr
  706. = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
  707. if (elf_tdata (abfd)->phdr == NULL)
  708. goto got_no_match;
  709. if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_phoff, SEEK_SET) != 0)
  710. goto got_no_match;
  711. i_phdr = elf_tdata (abfd)->phdr;
  712. for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
  713. {
  714. Elf_External_Phdr x_phdr;
  715. if (bfd_bread (&x_phdr, sizeof x_phdr, abfd) != sizeof x_phdr)
  716. goto got_no_match;
  717. elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
  718. /* Too much code in BFD relies on alignment being a power of
  719. two, as required by the ELF spec. */
  720. if (i_phdr->p_align != (i_phdr->p_align & -i_phdr->p_align))
  721. {
  722. abfd->read_only = 1;
  723. _bfd_error_handler (_("warning: %pB has a program header "
  724. "with invalid alignment"), abfd);
  725. }
  726. }
  727. }
  728. if (i_ehdrp->e_shstrndx != 0 && i_ehdrp->e_shoff >= sizeof (x_ehdr))
  729. {
  730. unsigned int num_sec;
  731. /* Once all of the section headers have been read and converted, we
  732. can start processing them. Note that the first section header is
  733. a dummy placeholder entry, so we ignore it. */
  734. num_sec = elf_numsections (abfd);
  735. for (shindex = 1; shindex < num_sec; shindex++)
  736. if (!bfd_section_from_shdr (abfd, shindex))
  737. goto got_no_match;
  738. /* Set up ELF sections for SHF_GROUP and SHF_LINK_ORDER. */
  739. if (! _bfd_elf_setup_sections (abfd))
  740. goto got_wrong_format_error;
  741. }
  742. /* Let the backend double check the format and override global
  743. information. */
  744. if (ebd->elf_backend_object_p)
  745. {
  746. if (! (*ebd->elf_backend_object_p) (abfd))
  747. goto got_wrong_format_error;
  748. }
  749. /* Remember the entry point specified in the ELF file header. */
  750. bfd_set_start_address (abfd, i_ehdrp->e_entry);
  751. /* If we have created any reloc sections that are associated with
  752. debugging sections, mark the reloc sections as debugging as well. */
  753. for (s = abfd->sections; s != NULL; s = s->next)
  754. {
  755. if ((elf_section_data (s)->this_hdr.sh_type == SHT_REL
  756. || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
  757. && elf_section_data (s)->this_hdr.sh_info > 0)
  758. {
  759. unsigned long targ_index;
  760. asection *targ_sec;
  761. targ_index = elf_section_data (s)->this_hdr.sh_info;
  762. targ_sec = bfd_section_from_elf_index (abfd, targ_index);
  763. if (targ_sec != NULL
  764. && (targ_sec->flags & SEC_DEBUGGING) != 0)
  765. s->flags |= SEC_DEBUGGING;
  766. }
  767. }
  768. return _bfd_no_cleanup;
  769. got_wrong_format_error:
  770. bfd_set_error (bfd_error_wrong_format);
  771. got_no_match:
  772. return NULL;
  773. }
  774. /* ELF .o/exec file writing */
  775. /* Write out the relocs. */
  776. void
  777. elf_write_relocs (bfd *abfd, asection *sec, void *data)
  778. {
  779. const struct elf_backend_data * const bed = get_elf_backend_data (abfd);
  780. bool *failedp = (bool *) data;
  781. Elf_Internal_Shdr *rela_hdr;
  782. bfd_vma addr_offset;
  783. void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
  784. size_t extsize;
  785. bfd_byte *dst_rela;
  786. unsigned int idx;
  787. asymbol *last_sym;
  788. int last_sym_idx;
  789. size_t amt;
  790. /* If we have already failed, don't do anything. */
  791. if (*failedp)
  792. return;
  793. if ((sec->flags & SEC_RELOC) == 0)
  794. return;
  795. /* The linker backend writes the relocs out itself, and sets the
  796. reloc_count field to zero to inhibit writing them here. Also,
  797. sometimes the SEC_RELOC flag gets set even when there aren't any
  798. relocs. */
  799. if (sec->reloc_count == 0)
  800. return;
  801. /* If we have opened an existing file for update, reloc_count may be
  802. set even though we are not linking. In that case we have nothing
  803. to do. */
  804. if (sec->orelocation == NULL)
  805. return;
  806. rela_hdr = elf_section_data (sec)->rela.hdr;
  807. if (rela_hdr == NULL)
  808. rela_hdr = elf_section_data (sec)->rel.hdr;
  809. rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count;
  810. if (_bfd_mul_overflow (sec->reloc_count, rela_hdr->sh_entsize, &amt)
  811. || (rela_hdr->contents = bfd_alloc (abfd, amt)) == NULL)
  812. {
  813. bfd_set_error (bfd_error_no_memory);
  814. *failedp = true;
  815. return;
  816. }
  817. /* Figure out whether the relocations are RELA or REL relocations. */
  818. if (rela_hdr->sh_type == SHT_RELA)
  819. {
  820. swap_out = elf_swap_reloca_out;
  821. extsize = sizeof (Elf_External_Rela);
  822. }
  823. else if (rela_hdr->sh_type == SHT_REL)
  824. {
  825. swap_out = elf_swap_reloc_out;
  826. extsize = sizeof (Elf_External_Rel);
  827. }
  828. else
  829. /* Every relocation section should be either an SHT_RELA or an
  830. SHT_REL section. */
  831. abort ();
  832. /* The address of an ELF reloc is section relative for an object
  833. file, and absolute for an executable file or shared library.
  834. The address of a BFD reloc is always section relative. */
  835. addr_offset = 0;
  836. if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
  837. addr_offset = sec->vma;
  838. /* orelocation has the data, reloc_count has the count... */
  839. last_sym = 0;
  840. last_sym_idx = 0;
  841. dst_rela = rela_hdr->contents;
  842. for (idx = 0; idx < sec->reloc_count; idx++, dst_rela += extsize)
  843. {
  844. Elf_Internal_Rela src_rela;
  845. arelent *ptr;
  846. asymbol *sym;
  847. int n;
  848. ptr = sec->orelocation[idx];
  849. sym = *ptr->sym_ptr_ptr;
  850. if (sym == last_sym)
  851. n = last_sym_idx;
  852. else if (bfd_is_abs_section (sym->section) && sym->value == 0)
  853. n = STN_UNDEF;
  854. else
  855. {
  856. last_sym = sym;
  857. n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
  858. if (n < 0)
  859. {
  860. *failedp = true;
  861. return;
  862. }
  863. last_sym_idx = n;
  864. }
  865. if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
  866. && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
  867. && ! _bfd_elf_validate_reloc (abfd, ptr))
  868. {
  869. *failedp = true;
  870. return;
  871. }
  872. if (ptr->howto == NULL)
  873. {
  874. *failedp = true;
  875. return;
  876. }
  877. #if defined(BFD64) && ARCH_SIZE == 32
  878. if (rela_hdr->sh_type == SHT_RELA
  879. && ptr->howto->bitsize > 32
  880. && ptr->addend - INT32_MIN > UINT32_MAX)
  881. {
  882. _bfd_error_handler (_("%pB: %pA+%"BFD_VMA_FMT"x: "
  883. "relocation addend %"BFD_VMA_FMT"x too large"),
  884. abfd, sec, ptr->address, ptr->addend);
  885. *failedp = true;
  886. bfd_set_error (bfd_error_bad_value);
  887. }
  888. #endif
  889. src_rela.r_offset = ptr->address + addr_offset;
  890. src_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
  891. src_rela.r_addend = ptr->addend;
  892. (*swap_out) (abfd, &src_rela, dst_rela);
  893. }
  894. if (elf_section_data (sec)->has_secondary_relocs
  895. && !bed->write_secondary_relocs (abfd, sec))
  896. {
  897. *failedp = true;
  898. return;
  899. }
  900. }
  901. /* Write out the program headers. */
  902. int
  903. elf_write_out_phdrs (bfd *abfd,
  904. const Elf_Internal_Phdr *phdr,
  905. unsigned int count)
  906. {
  907. while (count--)
  908. {
  909. Elf_External_Phdr extphdr;
  910. elf_swap_phdr_out (abfd, phdr, &extphdr);
  911. if (bfd_bwrite (&extphdr, sizeof (Elf_External_Phdr), abfd)
  912. != sizeof (Elf_External_Phdr))
  913. return -1;
  914. phdr++;
  915. }
  916. return 0;
  917. }
  918. /* Write out the section headers and the ELF file header. */
  919. bool
  920. elf_write_shdrs_and_ehdr (bfd *abfd)
  921. {
  922. Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
  923. Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
  924. Elf_External_Shdr *x_shdrp; /* Section header table, external form */
  925. Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
  926. unsigned int count;
  927. size_t amt;
  928. i_ehdrp = elf_elfheader (abfd);
  929. i_shdrp = elf_elfsections (abfd);
  930. /* swap the header before spitting it out... */
  931. #if DEBUG & 1
  932. elf_debug_file (i_ehdrp);
  933. #endif
  934. elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
  935. amt = sizeof (x_ehdr);
  936. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
  937. || bfd_bwrite (&x_ehdr, amt, abfd) != amt)
  938. return false;
  939. /* Some fields in the first section header handle overflow of ehdr
  940. fields. */
  941. if (i_ehdrp->e_phnum >= PN_XNUM)
  942. i_shdrp[0]->sh_info = i_ehdrp->e_phnum;
  943. if (i_ehdrp->e_shnum >= (SHN_LORESERVE & 0xffff))
  944. i_shdrp[0]->sh_size = i_ehdrp->e_shnum;
  945. if (i_ehdrp->e_shstrndx >= (SHN_LORESERVE & 0xffff))
  946. i_shdrp[0]->sh_link = i_ehdrp->e_shstrndx;
  947. /* at this point we've concocted all the ELF sections... */
  948. if (_bfd_mul_overflow (i_ehdrp->e_shnum, sizeof (*x_shdrp), &amt))
  949. {
  950. bfd_set_error (bfd_error_no_memory);
  951. return false;
  952. }
  953. x_shdrp = (Elf_External_Shdr *) bfd_alloc (abfd, amt);
  954. if (!x_shdrp)
  955. return false;
  956. for (count = 0; count < i_ehdrp->e_shnum; i_shdrp++, count++)
  957. {
  958. #if DEBUG & 2
  959. elf_debug_section (count, *i_shdrp);
  960. #endif
  961. elf_swap_shdr_out (abfd, *i_shdrp, x_shdrp + count);
  962. }
  963. amt = (bfd_size_type) i_ehdrp->e_shnum * sizeof (*x_shdrp);
  964. if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_shoff, SEEK_SET) != 0
  965. || bfd_bwrite (x_shdrp, amt, abfd) != amt)
  966. return false;
  967. /* need to dump the string table too... */
  968. return true;
  969. }
  970. bool
  971. elf_checksum_contents (bfd *abfd,
  972. void (*process) (const void *, size_t, void *),
  973. void *arg)
  974. {
  975. Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
  976. Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
  977. Elf_Internal_Phdr *i_phdrp = elf_tdata (abfd)->phdr;
  978. unsigned int count, num;
  979. {
  980. Elf_External_Ehdr x_ehdr;
  981. Elf_Internal_Ehdr i_ehdr;
  982. i_ehdr = *i_ehdrp;
  983. i_ehdr.e_phoff = i_ehdr.e_shoff = 0;
  984. elf_swap_ehdr_out (abfd, &i_ehdr, &x_ehdr);
  985. (*process) (&x_ehdr, sizeof x_ehdr, arg);
  986. }
  987. num = i_ehdrp->e_phnum;
  988. for (count = 0; count < num; count++)
  989. {
  990. Elf_External_Phdr x_phdr;
  991. elf_swap_phdr_out (abfd, &i_phdrp[count], &x_phdr);
  992. (*process) (&x_phdr, sizeof x_phdr, arg);
  993. }
  994. num = elf_numsections (abfd);
  995. for (count = 0; count < num; count++)
  996. {
  997. Elf_Internal_Shdr i_shdr;
  998. Elf_External_Shdr x_shdr;
  999. bfd_byte *contents, *free_contents;
  1000. i_shdr = *i_shdrp[count];
  1001. i_shdr.sh_offset = 0;
  1002. elf_swap_shdr_out (abfd, &i_shdr, &x_shdr);
  1003. (*process) (&x_shdr, sizeof x_shdr, arg);
  1004. /* Process the section's contents, if it has some.
  1005. PR ld/12451: Read them in if necessary. */
  1006. if (i_shdr.sh_type == SHT_NOBITS)
  1007. continue;
  1008. free_contents = NULL;
  1009. contents = i_shdr.contents;
  1010. if (contents == NULL)
  1011. {
  1012. asection *sec;
  1013. sec = bfd_section_from_elf_index (abfd, count);
  1014. if (sec != NULL)
  1015. {
  1016. contents = sec->contents;
  1017. if (contents == NULL)
  1018. {
  1019. /* Force rereading from file. */
  1020. sec->flags &= ~SEC_IN_MEMORY;
  1021. if (!bfd_malloc_and_get_section (abfd, sec, &free_contents))
  1022. continue;
  1023. contents = free_contents;
  1024. }
  1025. }
  1026. }
  1027. if (contents != NULL)
  1028. {
  1029. (*process) (contents, i_shdr.sh_size, arg);
  1030. free (free_contents);
  1031. }
  1032. }
  1033. return true;
  1034. }
  1035. long
  1036. elf_slurp_symbol_table (bfd *abfd, asymbol **symptrs, bool dynamic)
  1037. {
  1038. Elf_Internal_Shdr *hdr;
  1039. Elf_Internal_Shdr *verhdr;
  1040. unsigned long symcount; /* Number of external ELF symbols */
  1041. elf_symbol_type *sym; /* Pointer to current bfd symbol */
  1042. elf_symbol_type *symbase; /* Buffer for generated bfd symbols */
  1043. Elf_Internal_Sym *isym;
  1044. Elf_Internal_Sym *isymend;
  1045. Elf_Internal_Sym *isymbuf = NULL;
  1046. Elf_External_Versym *xver;
  1047. Elf_External_Versym *xverbuf = NULL;
  1048. const struct elf_backend_data *ebd;
  1049. size_t amt;
  1050. /* Read each raw ELF symbol, converting from external ELF form to
  1051. internal ELF form, and then using the information to create a
  1052. canonical bfd symbol table entry.
  1053. Note that we allocate the initial bfd canonical symbol buffer
  1054. based on a one-to-one mapping of the ELF symbols to canonical
  1055. symbols. We actually use all the ELF symbols, so there will be no
  1056. space left over at the end. When we have all the symbols, we
  1057. build the caller's pointer vector. */
  1058. if (! dynamic)
  1059. {
  1060. hdr = &elf_tdata (abfd)->symtab_hdr;
  1061. verhdr = NULL;
  1062. }
  1063. else
  1064. {
  1065. hdr = &elf_tdata (abfd)->dynsymtab_hdr;
  1066. if (elf_dynversym (abfd) == 0)
  1067. verhdr = NULL;
  1068. else
  1069. verhdr = &elf_tdata (abfd)->dynversym_hdr;
  1070. if ((elf_dynverdef (abfd) != 0
  1071. && elf_tdata (abfd)->verdef == NULL)
  1072. || (elf_dynverref (abfd) != 0
  1073. && elf_tdata (abfd)->verref == NULL))
  1074. {
  1075. if (!_bfd_elf_slurp_version_tables (abfd, false))
  1076. return -1;
  1077. }
  1078. }
  1079. ebd = get_elf_backend_data (abfd);
  1080. symcount = hdr->sh_size / sizeof (Elf_External_Sym);
  1081. if (symcount == 0)
  1082. sym = symbase = NULL;
  1083. else
  1084. {
  1085. isymbuf = bfd_elf_get_elf_syms (abfd, hdr, symcount, 0,
  1086. NULL, NULL, NULL);
  1087. if (isymbuf == NULL)
  1088. return -1;
  1089. if (_bfd_mul_overflow (symcount, sizeof (elf_symbol_type), &amt))
  1090. {
  1091. bfd_set_error (bfd_error_file_too_big);
  1092. goto error_return;
  1093. }
  1094. symbase = (elf_symbol_type *) bfd_zalloc (abfd, amt);
  1095. if (symbase == (elf_symbol_type *) NULL)
  1096. goto error_return;
  1097. /* Read the raw ELF version symbol information. */
  1098. if (verhdr != NULL
  1099. && verhdr->sh_size / sizeof (Elf_External_Versym) != symcount)
  1100. {
  1101. _bfd_error_handler
  1102. /* xgettext:c-format */
  1103. (_("%pB: version count (%" PRId64 ")"
  1104. " does not match symbol count (%ld)"),
  1105. abfd,
  1106. (int64_t) (verhdr->sh_size / sizeof (Elf_External_Versym)),
  1107. symcount);
  1108. /* Slurp in the symbols without the version information,
  1109. since that is more helpful than just quitting. */
  1110. verhdr = NULL;
  1111. }
  1112. if (verhdr != NULL)
  1113. {
  1114. if (bfd_seek (abfd, verhdr->sh_offset, SEEK_SET) != 0)
  1115. goto error_return;
  1116. xverbuf = (Elf_External_Versym *)
  1117. _bfd_malloc_and_read (abfd, verhdr->sh_size, verhdr->sh_size);
  1118. if (xverbuf == NULL && verhdr->sh_size != 0)
  1119. goto error_return;
  1120. }
  1121. /* Skip first symbol, which is a null dummy. */
  1122. xver = xverbuf;
  1123. if (xver != NULL)
  1124. ++xver;
  1125. isymend = isymbuf + symcount;
  1126. for (isym = isymbuf + 1, sym = symbase; isym < isymend; isym++, sym++)
  1127. {
  1128. memcpy (&sym->internal_elf_sym, isym, sizeof (Elf_Internal_Sym));
  1129. sym->symbol.the_bfd = abfd;
  1130. sym->symbol.name = bfd_elf_sym_name (abfd, hdr, isym, NULL);
  1131. sym->symbol.value = isym->st_value;
  1132. if (isym->st_shndx == SHN_UNDEF)
  1133. {
  1134. sym->symbol.section = bfd_und_section_ptr;
  1135. }
  1136. else if (isym->st_shndx == SHN_ABS)
  1137. {
  1138. sym->symbol.section = bfd_abs_section_ptr;
  1139. }
  1140. else if (isym->st_shndx == SHN_COMMON)
  1141. {
  1142. sym->symbol.section = bfd_com_section_ptr;
  1143. if ((abfd->flags & BFD_PLUGIN) != 0)
  1144. {
  1145. asection *xc = bfd_get_section_by_name (abfd, "COMMON");
  1146. if (xc == NULL)
  1147. {
  1148. flagword flags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
  1149. | SEC_EXCLUDE);
  1150. xc = bfd_make_section_with_flags (abfd, "COMMON", flags);
  1151. if (xc == NULL)
  1152. goto error_return;
  1153. }
  1154. sym->symbol.section = xc;
  1155. }
  1156. /* Elf puts the alignment into the `value' field, and
  1157. the size into the `size' field. BFD wants to see the
  1158. size in the value field, and doesn't care (at the
  1159. moment) about the alignment. */
  1160. sym->symbol.value = isym->st_size;
  1161. }
  1162. else
  1163. {
  1164. sym->symbol.section
  1165. = bfd_section_from_elf_index (abfd, isym->st_shndx);
  1166. if (sym->symbol.section == NULL)
  1167. {
  1168. /* This symbol is in a section for which we did not
  1169. create a BFD section. Just use bfd_abs_section,
  1170. although it is wrong. FIXME. Note - there is
  1171. code in elf.c:swap_out_syms that calls
  1172. symbol_section_index() in the elf backend for
  1173. cases like this. */
  1174. sym->symbol.section = bfd_abs_section_ptr;
  1175. }
  1176. }
  1177. /* If this is a relocatable file, then the symbol value is
  1178. already section relative. */
  1179. if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
  1180. sym->symbol.value -= sym->symbol.section->vma;
  1181. switch (ELF_ST_BIND (isym->st_info))
  1182. {
  1183. case STB_LOCAL:
  1184. sym->symbol.flags |= BSF_LOCAL;
  1185. break;
  1186. case STB_GLOBAL:
  1187. if (isym->st_shndx != SHN_UNDEF && isym->st_shndx != SHN_COMMON)
  1188. sym->symbol.flags |= BSF_GLOBAL;
  1189. break;
  1190. case STB_WEAK:
  1191. sym->symbol.flags |= BSF_WEAK;
  1192. break;
  1193. case STB_GNU_UNIQUE:
  1194. sym->symbol.flags |= BSF_GNU_UNIQUE;
  1195. break;
  1196. }
  1197. switch (ELF_ST_TYPE (isym->st_info))
  1198. {
  1199. case STT_SECTION:
  1200. /* Mark the input section symbol as used since it may be
  1201. used for relocation and section group.
  1202. NB: BSF_SECTION_SYM_USED is ignored by linker and may
  1203. be cleared by objcopy for non-relocatable inputs. */
  1204. sym->symbol.flags |= (BSF_SECTION_SYM
  1205. | BSF_DEBUGGING
  1206. | BSF_SECTION_SYM_USED);
  1207. break;
  1208. case STT_FILE:
  1209. sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING;
  1210. break;
  1211. case STT_FUNC:
  1212. sym->symbol.flags |= BSF_FUNCTION;
  1213. break;
  1214. case STT_COMMON:
  1215. /* FIXME: Do we have to put the size field into the value field
  1216. as we do with symbols in SHN_COMMON sections (see above) ? */
  1217. sym->symbol.flags |= BSF_ELF_COMMON;
  1218. /* Fall through. */
  1219. case STT_OBJECT:
  1220. sym->symbol.flags |= BSF_OBJECT;
  1221. break;
  1222. case STT_TLS:
  1223. sym->symbol.flags |= BSF_THREAD_LOCAL;
  1224. break;
  1225. case STT_RELC:
  1226. sym->symbol.flags |= BSF_RELC;
  1227. break;
  1228. case STT_SRELC:
  1229. sym->symbol.flags |= BSF_SRELC;
  1230. break;
  1231. case STT_GNU_IFUNC:
  1232. sym->symbol.flags |= BSF_GNU_INDIRECT_FUNCTION;
  1233. break;
  1234. }
  1235. if (dynamic)
  1236. sym->symbol.flags |= BSF_DYNAMIC;
  1237. if (xver != NULL)
  1238. {
  1239. Elf_Internal_Versym iversym;
  1240. _bfd_elf_swap_versym_in (abfd, xver, &iversym);
  1241. sym->version = iversym.vs_vers;
  1242. xver++;
  1243. }
  1244. /* Do some backend-specific processing on this symbol. */
  1245. if (ebd->elf_backend_symbol_processing)
  1246. (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
  1247. }
  1248. }
  1249. /* Do some backend-specific processing on this symbol table. */
  1250. if (ebd->elf_backend_symbol_table_processing)
  1251. (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount);
  1252. /* We rely on the zalloc to clear out the final symbol entry. */
  1253. symcount = sym - symbase;
  1254. /* Fill in the user's symbol pointer vector if needed. */
  1255. if (symptrs)
  1256. {
  1257. long l = symcount;
  1258. sym = symbase;
  1259. while (l-- > 0)
  1260. {
  1261. *symptrs++ = &sym->symbol;
  1262. sym++;
  1263. }
  1264. *symptrs = 0; /* Final null pointer */
  1265. }
  1266. free (xverbuf);
  1267. if (hdr->contents != (unsigned char *) isymbuf)
  1268. free (isymbuf);
  1269. return symcount;
  1270. error_return:
  1271. free (xverbuf);
  1272. if (hdr->contents != (unsigned char *) isymbuf)
  1273. free (isymbuf);
  1274. return -1;
  1275. }
  1276. /* Read relocations for ASECT from REL_HDR. There are RELOC_COUNT of
  1277. them. */
  1278. static bool
  1279. elf_slurp_reloc_table_from_section (bfd *abfd,
  1280. asection *asect,
  1281. Elf_Internal_Shdr *rel_hdr,
  1282. bfd_size_type reloc_count,
  1283. arelent *relents,
  1284. asymbol **symbols,
  1285. bool dynamic)
  1286. {
  1287. const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
  1288. void *allocated = NULL;
  1289. bfd_byte *native_relocs;
  1290. arelent *relent;
  1291. unsigned int i;
  1292. int entsize;
  1293. unsigned int symcount;
  1294. if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0)
  1295. return false;
  1296. allocated = _bfd_malloc_and_read (abfd, rel_hdr->sh_size, rel_hdr->sh_size);
  1297. if (allocated == NULL)
  1298. return false;
  1299. native_relocs = (bfd_byte *) allocated;
  1300. entsize = rel_hdr->sh_entsize;
  1301. BFD_ASSERT (entsize == sizeof (Elf_External_Rel)
  1302. || entsize == sizeof (Elf_External_Rela));
  1303. if (dynamic)
  1304. symcount = bfd_get_dynamic_symcount (abfd);
  1305. else
  1306. symcount = bfd_get_symcount (abfd);
  1307. for (i = 0, relent = relents;
  1308. i < reloc_count;
  1309. i++, relent++, native_relocs += entsize)
  1310. {
  1311. bool res;
  1312. Elf_Internal_Rela rela;
  1313. if (entsize == sizeof (Elf_External_Rela))
  1314. elf_swap_reloca_in (abfd, native_relocs, &rela);
  1315. else
  1316. elf_swap_reloc_in (abfd, native_relocs, &rela);
  1317. /* The address of an ELF reloc is section relative for an object
  1318. file, and absolute for an executable file or shared library.
  1319. The address of a normal BFD reloc is always section relative,
  1320. and the address of a dynamic reloc is absolute.. */
  1321. if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic)
  1322. relent->address = rela.r_offset;
  1323. else
  1324. relent->address = rela.r_offset - asect->vma;
  1325. if (ELF_R_SYM (rela.r_info) == STN_UNDEF)
  1326. /* FIXME: This and the error case below mean that we have a
  1327. symbol on relocs that is not elf_symbol_type. */
  1328. relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  1329. else if (ELF_R_SYM (rela.r_info) > symcount)
  1330. {
  1331. _bfd_error_handler
  1332. /* xgettext:c-format */
  1333. (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
  1334. abfd, asect, i, (long) ELF_R_SYM (rela.r_info));
  1335. bfd_set_error (bfd_error_bad_value);
  1336. relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  1337. }
  1338. else
  1339. {
  1340. asymbol **ps;
  1341. ps = symbols + ELF_R_SYM (rela.r_info) - 1;
  1342. relent->sym_ptr_ptr = ps;
  1343. }
  1344. relent->addend = rela.r_addend;
  1345. if ((entsize == sizeof (Elf_External_Rela)
  1346. && ebd->elf_info_to_howto != NULL)
  1347. || ebd->elf_info_to_howto_rel == NULL)
  1348. res = ebd->elf_info_to_howto (abfd, relent, &rela);
  1349. else
  1350. res = ebd->elf_info_to_howto_rel (abfd, relent, &rela);
  1351. if (! res || relent->howto == NULL)
  1352. goto error_return;
  1353. }
  1354. free (allocated);
  1355. return true;
  1356. error_return:
  1357. free (allocated);
  1358. return false;
  1359. }
  1360. /* Read in and swap the external relocs. */
  1361. bool
  1362. elf_slurp_reloc_table (bfd *abfd,
  1363. asection *asect,
  1364. asymbol **symbols,
  1365. bool dynamic)
  1366. {
  1367. const struct elf_backend_data * const bed = get_elf_backend_data (abfd);
  1368. struct bfd_elf_section_data * const d = elf_section_data (asect);
  1369. Elf_Internal_Shdr *rel_hdr;
  1370. Elf_Internal_Shdr *rel_hdr2;
  1371. bfd_size_type reloc_count;
  1372. bfd_size_type reloc_count2;
  1373. arelent *relents;
  1374. size_t amt;
  1375. if (asect->relocation != NULL)
  1376. return true;
  1377. if (! dynamic)
  1378. {
  1379. if ((asect->flags & SEC_RELOC) == 0
  1380. || asect->reloc_count == 0)
  1381. return true;
  1382. rel_hdr = d->rel.hdr;
  1383. reloc_count = rel_hdr ? NUM_SHDR_ENTRIES (rel_hdr) : 0;
  1384. rel_hdr2 = d->rela.hdr;
  1385. reloc_count2 = rel_hdr2 ? NUM_SHDR_ENTRIES (rel_hdr2) : 0;
  1386. /* PR 17512: file: 0b4f81b7. */
  1387. if (asect->reloc_count != reloc_count + reloc_count2)
  1388. return false;
  1389. BFD_ASSERT ((rel_hdr && asect->rel_filepos == rel_hdr->sh_offset)
  1390. || (rel_hdr2 && asect->rel_filepos == rel_hdr2->sh_offset));
  1391. }
  1392. else
  1393. {
  1394. /* Note that ASECT->RELOC_COUNT tends not to be accurate in this
  1395. case because relocations against this section may use the
  1396. dynamic symbol table, and in that case bfd_section_from_shdr
  1397. in elf.c does not update the RELOC_COUNT. */
  1398. if (asect->size == 0)
  1399. return true;
  1400. rel_hdr = &d->this_hdr;
  1401. reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
  1402. rel_hdr2 = NULL;
  1403. reloc_count2 = 0;
  1404. }
  1405. if (_bfd_mul_overflow (reloc_count + reloc_count2, sizeof (arelent), &amt))
  1406. {
  1407. bfd_set_error (bfd_error_file_too_big);
  1408. return false;
  1409. }
  1410. relents = (arelent *) bfd_alloc (abfd, amt);
  1411. if (relents == NULL)
  1412. return false;
  1413. if (rel_hdr
  1414. && !elf_slurp_reloc_table_from_section (abfd, asect,
  1415. rel_hdr, reloc_count,
  1416. relents,
  1417. symbols, dynamic))
  1418. return false;
  1419. if (rel_hdr2
  1420. && !elf_slurp_reloc_table_from_section (abfd, asect,
  1421. rel_hdr2, reloc_count2,
  1422. relents + reloc_count,
  1423. symbols, dynamic))
  1424. return false;
  1425. if (!bed->slurp_secondary_relocs (abfd, asect, symbols, dynamic))
  1426. return false;
  1427. asect->relocation = relents;
  1428. return true;
  1429. }
  1430. #if DEBUG & 2
  1431. static void
  1432. elf_debug_section (int num, Elf_Internal_Shdr *hdr)
  1433. {
  1434. fprintf (stderr, "\nSection#%d '%s' 0x%.8lx\n", num,
  1435. hdr->bfd_section != NULL ? hdr->bfd_section->name : "",
  1436. (long) hdr);
  1437. fprintf (stderr,
  1438. "sh_name = %ld\tsh_type = %ld\tsh_flags = %ld\n",
  1439. (long) hdr->sh_name,
  1440. (long) hdr->sh_type,
  1441. (long) hdr->sh_flags);
  1442. fprintf (stderr,
  1443. "sh_addr = %ld\tsh_offset = %ld\tsh_size = %ld\n",
  1444. (long) hdr->sh_addr,
  1445. (long) hdr->sh_offset,
  1446. (long) hdr->sh_size);
  1447. fprintf (stderr,
  1448. "sh_link = %ld\tsh_info = %ld\tsh_addralign = %ld\n",
  1449. (long) hdr->sh_link,
  1450. (long) hdr->sh_info,
  1451. (long) hdr->sh_addralign);
  1452. fprintf (stderr, "sh_entsize = %ld\n",
  1453. (long) hdr->sh_entsize);
  1454. fflush (stderr);
  1455. }
  1456. #endif
  1457. #if DEBUG & 1
  1458. static void
  1459. elf_debug_file (Elf_Internal_Ehdr *ehdrp)
  1460. {
  1461. fprintf (stderr, "e_entry = 0x%.8lx\n", (long) ehdrp->e_entry);
  1462. fprintf (stderr, "e_phoff = %ld\n", (long) ehdrp->e_phoff);
  1463. fprintf (stderr, "e_phnum = %ld\n", (long) ehdrp->e_phnum);
  1464. fprintf (stderr, "e_phentsize = %ld\n", (long) ehdrp->e_phentsize);
  1465. fprintf (stderr, "e_shoff = %ld\n", (long) ehdrp->e_shoff);
  1466. fprintf (stderr, "e_shnum = %ld\n", (long) ehdrp->e_shnum);
  1467. fprintf (stderr, "e_shentsize = %ld\n", (long) ehdrp->e_shentsize);
  1468. }
  1469. #endif
  1470. /* Create a new BFD as if by bfd_openr. Rather than opening a file,
  1471. reconstruct an ELF file by reading the segments out of remote
  1472. memory based on the ELF file header at EHDR_VMA and the ELF program
  1473. headers it points to. If non-zero, SIZE is the known extent of the
  1474. object. If not null, *LOADBASEP is filled in with the difference
  1475. between the VMAs from which the segments were read, and the VMAs
  1476. the file headers (and hence BFD's idea of each section's VMA) put
  1477. them at.
  1478. The function TARGET_READ_MEMORY is called to copy LEN bytes from
  1479. the remote memory at target address VMA into the local buffer at
  1480. MYADDR; it should return zero on success or an `errno' code on
  1481. failure. TEMPL must be a BFD for a target with the word size and
  1482. byte order found in the remote memory. */
  1483. bfd *
  1484. NAME(_bfd_elf,bfd_from_remote_memory)
  1485. (bfd *templ,
  1486. bfd_vma ehdr_vma /* Bytes. */,
  1487. bfd_size_type size /* Octets. */,
  1488. bfd_vma *loadbasep /* Bytes. */,
  1489. int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
  1490. /* (Bytes , , octets ). */
  1491. {
  1492. Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
  1493. Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form */
  1494. Elf_External_Phdr *x_phdrs;
  1495. Elf_Internal_Phdr *i_phdrs, *last_phdr, *first_phdr;
  1496. bfd *nbfd;
  1497. struct bfd_in_memory *bim;
  1498. bfd_byte *contents;
  1499. int err;
  1500. unsigned int i;
  1501. bfd_vma high_offset;
  1502. bfd_vma shdr_end;
  1503. bfd_vma loadbase; /* Bytes. */
  1504. size_t amt;
  1505. unsigned int opb = bfd_octets_per_byte (templ, NULL);
  1506. /* Read in the ELF header in external format. */
  1507. err = target_read_memory (ehdr_vma, (bfd_byte *) &x_ehdr, sizeof x_ehdr);
  1508. if (err)
  1509. {
  1510. bfd_set_error (bfd_error_system_call);
  1511. errno = err;
  1512. return NULL;
  1513. }
  1514. /* Now check to see if we have a valid ELF file, and one that BFD can
  1515. make use of. The magic number must match, the address size ('class')
  1516. and byte-swapping must match our XVEC entry. */
  1517. if (! elf_file_p (&x_ehdr)
  1518. || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
  1519. || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
  1520. {
  1521. bfd_set_error (bfd_error_wrong_format);
  1522. return NULL;
  1523. }
  1524. /* Check that file's byte order matches xvec's */
  1525. switch (x_ehdr.e_ident[EI_DATA])
  1526. {
  1527. case ELFDATA2MSB: /* Big-endian */
  1528. if (! bfd_header_big_endian (templ))
  1529. {
  1530. bfd_set_error (bfd_error_wrong_format);
  1531. return NULL;
  1532. }
  1533. break;
  1534. case ELFDATA2LSB: /* Little-endian */
  1535. if (! bfd_header_little_endian (templ))
  1536. {
  1537. bfd_set_error (bfd_error_wrong_format);
  1538. return NULL;
  1539. }
  1540. break;
  1541. case ELFDATANONE: /* No data encoding specified */
  1542. default: /* Unknown data encoding specified */
  1543. bfd_set_error (bfd_error_wrong_format);
  1544. return NULL;
  1545. }
  1546. elf_swap_ehdr_in (templ, &x_ehdr, &i_ehdr);
  1547. /* The file header tells where to find the program headers.
  1548. These are what we use to actually choose what to read. */
  1549. if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
  1550. {
  1551. bfd_set_error (bfd_error_wrong_format);
  1552. return NULL;
  1553. }
  1554. if (_bfd_mul_overflow (i_ehdr.e_phnum,
  1555. sizeof (*x_phdrs) + sizeof (*i_phdrs), &amt))
  1556. {
  1557. bfd_set_error (bfd_error_file_too_big);
  1558. return NULL;
  1559. }
  1560. x_phdrs = (Elf_External_Phdr *) bfd_malloc (amt);
  1561. if (x_phdrs == NULL)
  1562. return NULL;
  1563. err = target_read_memory (ehdr_vma + i_ehdr.e_phoff, (bfd_byte *) x_phdrs,
  1564. i_ehdr.e_phnum * sizeof x_phdrs[0]);
  1565. if (err)
  1566. {
  1567. free (x_phdrs);
  1568. bfd_set_error (bfd_error_system_call);
  1569. errno = err;
  1570. return NULL;
  1571. }
  1572. i_phdrs = (Elf_Internal_Phdr *) &x_phdrs[i_ehdr.e_phnum];
  1573. high_offset = 0;
  1574. loadbase = 0;
  1575. first_phdr = NULL;
  1576. last_phdr = NULL;
  1577. for (i = 0; i < i_ehdr.e_phnum; ++i)
  1578. {
  1579. elf_swap_phdr_in (templ, &x_phdrs[i], &i_phdrs[i]);
  1580. if (i_phdrs[i].p_type == PT_LOAD)
  1581. {
  1582. bfd_vma segment_end = i_phdrs[i].p_offset + i_phdrs[i].p_filesz;
  1583. if (segment_end > high_offset)
  1584. {
  1585. high_offset = segment_end;
  1586. last_phdr = &i_phdrs[i];
  1587. }
  1588. /* If this program header covers offset zero, where the file
  1589. header sits, then we can figure out the loadbase. */
  1590. if (first_phdr == NULL)
  1591. {
  1592. bfd_vma p_offset = i_phdrs[i].p_offset; /* Octets. */
  1593. bfd_vma p_vaddr = i_phdrs[i].p_vaddr; /* Octets. */
  1594. if (i_phdrs[i].p_align > 1)
  1595. {
  1596. p_offset &= -(i_phdrs[i].p_align * opb);
  1597. p_vaddr &= -(i_phdrs[i].p_align * opb);
  1598. }
  1599. if (p_offset == 0)
  1600. {
  1601. loadbase = ehdr_vma - p_vaddr / opb;
  1602. first_phdr = &i_phdrs[i];
  1603. }
  1604. }
  1605. }
  1606. }
  1607. if (high_offset == 0)
  1608. {
  1609. /* There were no PT_LOAD segments, so we don't have anything to read. */
  1610. free (x_phdrs);
  1611. bfd_set_error (bfd_error_wrong_format);
  1612. return NULL;
  1613. }
  1614. shdr_end = 0;
  1615. if (i_ehdr.e_shoff != 0 && i_ehdr.e_shnum != 0 && i_ehdr.e_shentsize != 0)
  1616. {
  1617. shdr_end = i_ehdr.e_shoff + i_ehdr.e_shnum * i_ehdr.e_shentsize;
  1618. if (last_phdr->p_filesz != last_phdr->p_memsz)
  1619. {
  1620. /* If the last PT_LOAD header has a bss area then ld.so will
  1621. have cleared anything past p_filesz, zapping the section
  1622. headers. */
  1623. }
  1624. else if (size >= shdr_end)
  1625. high_offset = size;
  1626. else
  1627. {
  1628. bfd_vma page_size = get_elf_backend_data (templ)->minpagesize;
  1629. bfd_vma segment_end = last_phdr->p_offset + last_phdr->p_filesz;
  1630. /* Assume we loaded full pages, allowing us to sometimes see
  1631. section headers. */
  1632. if (page_size > 1 && shdr_end > segment_end)
  1633. {
  1634. bfd_vma page_end = (segment_end + page_size - 1) & -page_size;
  1635. if (page_end >= shdr_end)
  1636. /* Whee, section headers covered. */
  1637. high_offset = shdr_end;
  1638. }
  1639. }
  1640. }
  1641. /* Now we know the size of the whole image we want read in. */
  1642. contents = (bfd_byte *) bfd_zmalloc (high_offset);
  1643. if (contents == NULL)
  1644. {
  1645. free (x_phdrs);
  1646. return NULL;
  1647. }
  1648. for (i = 0; i < i_ehdr.e_phnum; ++i)
  1649. if (i_phdrs[i].p_type == PT_LOAD)
  1650. {
  1651. bfd_vma start = i_phdrs[i].p_offset; /* Octets. */
  1652. bfd_vma end = start + i_phdrs[i].p_filesz; /* Octets. */
  1653. bfd_vma vaddr = i_phdrs[i].p_vaddr; /* Octets. */
  1654. /* Extend the beginning of the first pt_load to cover file
  1655. header and program headers, if we proved earlier that its
  1656. aligned offset is 0. */
  1657. if (first_phdr == &i_phdrs[i])
  1658. {
  1659. vaddr -= start;
  1660. start = 0;
  1661. }
  1662. /* Extend the end of the last pt_load to cover section headers. */
  1663. if (last_phdr == &i_phdrs[i])
  1664. end = high_offset;
  1665. err = target_read_memory (loadbase + vaddr / opb,
  1666. contents + start, end - start);
  1667. if (err)
  1668. {
  1669. free (x_phdrs);
  1670. free (contents);
  1671. bfd_set_error (bfd_error_system_call);
  1672. errno = err;
  1673. return NULL;
  1674. }
  1675. }
  1676. free (x_phdrs);
  1677. /* If the segments visible in memory didn't include the section headers,
  1678. then clear them from the file header. */
  1679. if (high_offset < shdr_end)
  1680. {
  1681. memset (&x_ehdr.e_shoff, 0, sizeof x_ehdr.e_shoff);
  1682. memset (&x_ehdr.e_shnum, 0, sizeof x_ehdr.e_shnum);
  1683. memset (&x_ehdr.e_shstrndx, 0, sizeof x_ehdr.e_shstrndx);
  1684. }
  1685. /* This will normally have been in the first PT_LOAD segment. But it
  1686. conceivably could be missing, and we might have just changed it. */
  1687. memcpy (contents, &x_ehdr, sizeof x_ehdr);
  1688. /* Now we have a memory image of the ELF file contents. Make a BFD. */
  1689. bim = (struct bfd_in_memory *) bfd_malloc (sizeof (struct bfd_in_memory));
  1690. if (bim == NULL)
  1691. {
  1692. free (contents);
  1693. return NULL;
  1694. }
  1695. nbfd = _bfd_new_bfd ();
  1696. if (nbfd == NULL
  1697. || !bfd_set_filename (nbfd, "<in-memory>"))
  1698. {
  1699. free (bim);
  1700. free (contents);
  1701. return NULL;
  1702. }
  1703. nbfd->xvec = templ->xvec;
  1704. bim->size = high_offset;
  1705. bim->buffer = contents;
  1706. nbfd->iostream = bim;
  1707. nbfd->flags = BFD_IN_MEMORY;
  1708. nbfd->iovec = &_bfd_memory_iovec;
  1709. nbfd->origin = 0;
  1710. nbfd->direction = read_direction;
  1711. nbfd->mtime = time (NULL);
  1712. nbfd->mtime_set = true;
  1713. if (loadbasep)
  1714. *loadbasep = loadbase;
  1715. return nbfd;
  1716. }
  1717. /* Function for ELF_R_INFO. */
  1718. bfd_vma
  1719. NAME(elf,r_info) (bfd_vma sym, bfd_vma type)
  1720. {
  1721. return ELF_R_INFO (sym, type);
  1722. }
  1723. /* Function for ELF_R_SYM. */
  1724. bfd_vma
  1725. NAME(elf,r_sym) (bfd_vma r_info)
  1726. {
  1727. return ELF_R_SYM (r_info);
  1728. }
  1729. #include "elfcore.h"
  1730. /* Size-dependent data and functions. */
  1731. const struct elf_size_info NAME(_bfd_elf,size_info) = {
  1732. sizeof (Elf_External_Ehdr),
  1733. sizeof (Elf_External_Phdr),
  1734. sizeof (Elf_External_Shdr),
  1735. sizeof (Elf_External_Rel),
  1736. sizeof (Elf_External_Rela),
  1737. sizeof (Elf_External_Sym),
  1738. sizeof (Elf_External_Dyn),
  1739. sizeof (Elf_External_Note),
  1740. 4,
  1741. 1,
  1742. ARCH_SIZE, LOG_FILE_ALIGN,
  1743. ELFCLASS, EV_CURRENT,
  1744. elf_write_out_phdrs,
  1745. elf_write_shdrs_and_ehdr,
  1746. elf_checksum_contents,
  1747. elf_write_relocs,
  1748. elf_swap_symbol_in,
  1749. elf_swap_symbol_out,
  1750. elf_slurp_reloc_table,
  1751. elf_slurp_symbol_table,
  1752. elf_swap_dyn_in,
  1753. elf_swap_dyn_out,
  1754. elf_swap_reloc_in,
  1755. elf_swap_reloc_out,
  1756. elf_swap_reloca_in,
  1757. elf_swap_reloca_out
  1758. };