solib-svr4.c 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195
  1. /* Handle SVR4 shared libraries for GDB, the GNU Debugger.
  2. Copyright (C) 1990-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  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 "defs.h"
  15. #include "elf/external.h"
  16. #include "elf/common.h"
  17. #include "elf/mips.h"
  18. #include "symtab.h"
  19. #include "bfd.h"
  20. #include "symfile.h"
  21. #include "objfiles.h"
  22. #include "gdbcore.h"
  23. #include "target.h"
  24. #include "inferior.h"
  25. #include "infrun.h"
  26. #include "regcache.h"
  27. #include "gdbthread.h"
  28. #include "observable.h"
  29. #include "solist.h"
  30. #include "solib.h"
  31. #include "solib-svr4.h"
  32. #include "bfd-target.h"
  33. #include "elf-bfd.h"
  34. #include "exec.h"
  35. #include "auxv.h"
  36. #include "gdb_bfd.h"
  37. #include "probe.h"
  38. static struct link_map_offsets *svr4_fetch_link_map_offsets (void);
  39. static int svr4_have_link_map_offsets (void);
  40. static void svr4_relocate_main_executable (void);
  41. static void svr4_free_library_list (void *p_list);
  42. static void probes_table_remove_objfile_probes (struct objfile *objfile);
  43. static void svr4_iterate_over_objfiles_in_search_order (
  44. struct gdbarch *gdbarch, iterate_over_objfiles_in_search_order_cb_ftype *cb,
  45. void *cb_data, struct objfile *objfile);
  46. /* On SVR4 systems, a list of symbols in the dynamic linker where
  47. GDB can try to place a breakpoint to monitor shared library
  48. events.
  49. If none of these symbols are found, or other errors occur, then
  50. SVR4 systems will fall back to using a symbol as the "startup
  51. mapping complete" breakpoint address. */
  52. static const char * const solib_break_names[] =
  53. {
  54. "r_debug_state",
  55. "_r_debug_state",
  56. "_dl_debug_state",
  57. "rtld_db_dlactivity",
  58. "__dl_rtld_db_dlactivity",
  59. "_rtld_debug_state",
  60. NULL
  61. };
  62. static const char * const bkpt_names[] =
  63. {
  64. "_start",
  65. "__start",
  66. "main",
  67. NULL
  68. };
  69. static const char * const main_name_list[] =
  70. {
  71. "main_$main",
  72. NULL
  73. };
  74. /* What to do when a probe stop occurs. */
  75. enum probe_action
  76. {
  77. /* Something went seriously wrong. Stop using probes and
  78. revert to using the older interface. */
  79. PROBES_INTERFACE_FAILED,
  80. /* No action is required. The shared object list is still
  81. valid. */
  82. DO_NOTHING,
  83. /* The shared object list should be reloaded entirely. */
  84. FULL_RELOAD,
  85. /* Attempt to incrementally update the shared object list. If
  86. the update fails or is not possible, fall back to reloading
  87. the list in full. */
  88. UPDATE_OR_RELOAD,
  89. };
  90. /* A probe's name and its associated action. */
  91. struct probe_info
  92. {
  93. /* The name of the probe. */
  94. const char *name;
  95. /* What to do when a probe stop occurs. */
  96. enum probe_action action;
  97. };
  98. /* A list of named probes and their associated actions. If all
  99. probes are present in the dynamic linker then the probes-based
  100. interface will be used. */
  101. static const struct probe_info probe_info[] =
  102. {
  103. { "init_start", DO_NOTHING },
  104. { "init_complete", FULL_RELOAD },
  105. { "map_start", DO_NOTHING },
  106. { "map_failed", DO_NOTHING },
  107. { "reloc_complete", UPDATE_OR_RELOAD },
  108. { "unmap_start", DO_NOTHING },
  109. { "unmap_complete", FULL_RELOAD },
  110. };
  111. #define NUM_PROBES ARRAY_SIZE (probe_info)
  112. /* Return non-zero if GDB_SO_NAME and INFERIOR_SO_NAME represent
  113. the same shared library. */
  114. static int
  115. svr4_same_1 (const char *gdb_so_name, const char *inferior_so_name)
  116. {
  117. if (strcmp (gdb_so_name, inferior_so_name) == 0)
  118. return 1;
  119. /* On Solaris, when starting inferior we think that dynamic linker is
  120. /usr/lib/ld.so.1, but later on, the table of loaded shared libraries
  121. contains /lib/ld.so.1. Sometimes one file is a link to another, but
  122. sometimes they have identical content, but are not linked to each
  123. other. We don't restrict this check for Solaris, but the chances
  124. of running into this situation elsewhere are very low. */
  125. if (strcmp (gdb_so_name, "/usr/lib/ld.so.1") == 0
  126. && strcmp (inferior_so_name, "/lib/ld.so.1") == 0)
  127. return 1;
  128. /* Similarly, we observed the same issue with amd64 and sparcv9, but with
  129. different locations. */
  130. if (strcmp (gdb_so_name, "/usr/lib/amd64/ld.so.1") == 0
  131. && strcmp (inferior_so_name, "/lib/amd64/ld.so.1") == 0)
  132. return 1;
  133. if (strcmp (gdb_so_name, "/usr/lib/sparcv9/ld.so.1") == 0
  134. && strcmp (inferior_so_name, "/lib/sparcv9/ld.so.1") == 0)
  135. return 1;
  136. return 0;
  137. }
  138. static int
  139. svr4_same (struct so_list *gdb, struct so_list *inferior)
  140. {
  141. return (svr4_same_1 (gdb->so_original_name, inferior->so_original_name));
  142. }
  143. static std::unique_ptr<lm_info_svr4>
  144. lm_info_read (CORE_ADDR lm_addr)
  145. {
  146. struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
  147. std::unique_ptr<lm_info_svr4> lm_info;
  148. gdb::byte_vector lm (lmo->link_map_size);
  149. if (target_read_memory (lm_addr, lm.data (), lmo->link_map_size) != 0)
  150. warning (_("Error reading shared library list entry at %s"),
  151. paddress (target_gdbarch (), lm_addr));
  152. else
  153. {
  154. struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
  155. lm_info.reset (new lm_info_svr4);
  156. lm_info->lm_addr = lm_addr;
  157. lm_info->l_addr_inferior = extract_typed_address (&lm[lmo->l_addr_offset],
  158. ptr_type);
  159. lm_info->l_ld = extract_typed_address (&lm[lmo->l_ld_offset], ptr_type);
  160. lm_info->l_next = extract_typed_address (&lm[lmo->l_next_offset],
  161. ptr_type);
  162. lm_info->l_prev = extract_typed_address (&lm[lmo->l_prev_offset],
  163. ptr_type);
  164. lm_info->l_name = extract_typed_address (&lm[lmo->l_name_offset],
  165. ptr_type);
  166. }
  167. return lm_info;
  168. }
  169. static int
  170. has_lm_dynamic_from_link_map (void)
  171. {
  172. struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
  173. return lmo->l_ld_offset >= 0;
  174. }
  175. static CORE_ADDR
  176. lm_addr_check (const struct so_list *so, bfd *abfd)
  177. {
  178. lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
  179. if (!li->l_addr_p)
  180. {
  181. struct bfd_section *dyninfo_sect;
  182. CORE_ADDR l_addr, l_dynaddr, dynaddr;
  183. l_addr = li->l_addr_inferior;
  184. if (! abfd || ! has_lm_dynamic_from_link_map ())
  185. goto set_addr;
  186. l_dynaddr = li->l_ld;
  187. dyninfo_sect = bfd_get_section_by_name (abfd, ".dynamic");
  188. if (dyninfo_sect == NULL)
  189. goto set_addr;
  190. dynaddr = bfd_section_vma (dyninfo_sect);
  191. if (dynaddr + l_addr != l_dynaddr)
  192. {
  193. CORE_ADDR align = 0x1000;
  194. CORE_ADDR minpagesize = align;
  195. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  196. {
  197. Elf_Internal_Ehdr *ehdr = elf_tdata (abfd)->elf_header;
  198. Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr;
  199. int i;
  200. align = 1;
  201. for (i = 0; i < ehdr->e_phnum; i++)
  202. if (phdr[i].p_type == PT_LOAD && phdr[i].p_align > align)
  203. align = phdr[i].p_align;
  204. minpagesize = get_elf_backend_data (abfd)->minpagesize;
  205. }
  206. /* Turn it into a mask. */
  207. align--;
  208. /* If the changes match the alignment requirements, we
  209. assume we're using a core file that was generated by the
  210. same binary, just prelinked with a different base offset.
  211. If it doesn't match, we may have a different binary, the
  212. same binary with the dynamic table loaded at an unrelated
  213. location, or anything, really. To avoid regressions,
  214. don't adjust the base offset in the latter case, although
  215. odds are that, if things really changed, debugging won't
  216. quite work.
  217. One could expect more the condition
  218. ((l_addr & align) == 0 && ((l_dynaddr - dynaddr) & align) == 0)
  219. but the one below is relaxed for PPC. The PPC kernel supports
  220. either 4k or 64k page sizes. To be prepared for 64k pages,
  221. PPC ELF files are built using an alignment requirement of 64k.
  222. However, when running on a kernel supporting 4k pages, the memory
  223. mapping of the library may not actually happen on a 64k boundary!
  224. (In the usual case where (l_addr & align) == 0, this check is
  225. equivalent to the possibly expected check above.)
  226. Even on PPC it must be zero-aligned at least for MINPAGESIZE. */
  227. l_addr = l_dynaddr - dynaddr;
  228. if ((l_addr & (minpagesize - 1)) == 0
  229. && (l_addr & align) == ((l_dynaddr - dynaddr) & align))
  230. {
  231. if (info_verbose)
  232. gdb_printf (_("Using PIC (Position Independent Code) "
  233. "prelink displacement %s for \"%s\".\n"),
  234. paddress (target_gdbarch (), l_addr),
  235. so->so_name);
  236. }
  237. else
  238. {
  239. /* There is no way to verify the library file matches. prelink
  240. can during prelinking of an unprelinked file (or unprelinking
  241. of a prelinked file) shift the DYNAMIC segment by arbitrary
  242. offset without any page size alignment. There is no way to
  243. find out the ELF header and/or Program Headers for a limited
  244. verification if it they match. One could do a verification
  245. of the DYNAMIC segment. Still the found address is the best
  246. one GDB could find. */
  247. warning (_(".dynamic section for \"%s\" "
  248. "is not at the expected address "
  249. "(wrong library or version mismatch?)"), so->so_name);
  250. }
  251. }
  252. set_addr:
  253. li->l_addr = l_addr;
  254. li->l_addr_p = 1;
  255. }
  256. return li->l_addr;
  257. }
  258. /* Per pspace SVR4 specific data. */
  259. struct svr4_info
  260. {
  261. svr4_info () = default;
  262. ~svr4_info ();
  263. /* Base of dynamic linker structures. */
  264. CORE_ADDR debug_base = 0;
  265. /* Validity flag for debug_loader_offset. */
  266. int debug_loader_offset_p = 0;
  267. /* Load address for the dynamic linker, inferred. */
  268. CORE_ADDR debug_loader_offset = 0;
  269. /* Name of the dynamic linker, valid if debug_loader_offset_p. */
  270. char *debug_loader_name = nullptr;
  271. /* Load map address for the main executable. */
  272. CORE_ADDR main_lm_addr = 0;
  273. CORE_ADDR interp_text_sect_low = 0;
  274. CORE_ADDR interp_text_sect_high = 0;
  275. CORE_ADDR interp_plt_sect_low = 0;
  276. CORE_ADDR interp_plt_sect_high = 0;
  277. /* Nonzero if the list of objects was last obtained from the target
  278. via qXfer:libraries-svr4:read. */
  279. int using_xfer = 0;
  280. /* Table of struct probe_and_action instances, used by the
  281. probes-based interface to map breakpoint addresses to probes
  282. and their associated actions. Lookup is performed using
  283. probe_and_action->prob->address. */
  284. htab_up probes_table;
  285. /* List of objects loaded into the inferior, used by the probes-
  286. based interface. */
  287. struct so_list *solib_list = nullptr;
  288. };
  289. /* Per-program-space data key. */
  290. static const struct program_space_key<svr4_info> solib_svr4_pspace_data;
  291. /* Free the probes table. */
  292. static void
  293. free_probes_table (struct svr4_info *info)
  294. {
  295. info->probes_table.reset (nullptr);
  296. }
  297. /* Free the solib list. */
  298. static void
  299. free_solib_list (struct svr4_info *info)
  300. {
  301. svr4_free_library_list (&info->solib_list);
  302. info->solib_list = NULL;
  303. }
  304. svr4_info::~svr4_info ()
  305. {
  306. free_solib_list (this);
  307. }
  308. /* Get the svr4 data for program space PSPACE. If none is found yet, add it now.
  309. This function always returns a valid object. */
  310. static struct svr4_info *
  311. get_svr4_info (program_space *pspace)
  312. {
  313. struct svr4_info *info = solib_svr4_pspace_data.get (pspace);
  314. if (info == NULL)
  315. info = solib_svr4_pspace_data.emplace (pspace);
  316. return info;
  317. }
  318. /* Local function prototypes */
  319. static int match_main (const char *);
  320. /* Read program header TYPE from inferior memory. The header is found
  321. by scanning the OS auxiliary vector.
  322. If TYPE == -1, return the program headers instead of the contents of
  323. one program header.
  324. Return vector of bytes holding the program header contents, or an empty
  325. optional on failure. If successful and P_ARCH_SIZE is non-NULL, the target
  326. architecture size (32-bit or 64-bit) is returned to *P_ARCH_SIZE. Likewise,
  327. the base address of the section is returned in *BASE_ADDR. */
  328. static gdb::optional<gdb::byte_vector>
  329. read_program_header (int type, int *p_arch_size, CORE_ADDR *base_addr)
  330. {
  331. enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  332. CORE_ADDR at_phdr, at_phent, at_phnum, pt_phdr = 0;
  333. int arch_size, sect_size;
  334. CORE_ADDR sect_addr;
  335. int pt_phdr_p = 0;
  336. /* Get required auxv elements from target. */
  337. if (target_auxv_search (current_inferior ()->top_target (),
  338. AT_PHDR, &at_phdr) <= 0)
  339. return {};
  340. if (target_auxv_search (current_inferior ()->top_target (),
  341. AT_PHENT, &at_phent) <= 0)
  342. return {};
  343. if (target_auxv_search (current_inferior ()->top_target (),
  344. AT_PHNUM, &at_phnum) <= 0)
  345. return {};
  346. if (!at_phdr || !at_phnum)
  347. return {};
  348. /* Determine ELF architecture type. */
  349. if (at_phent == sizeof (Elf32_External_Phdr))
  350. arch_size = 32;
  351. else if (at_phent == sizeof (Elf64_External_Phdr))
  352. arch_size = 64;
  353. else
  354. return {};
  355. /* Find the requested segment. */
  356. if (type == -1)
  357. {
  358. sect_addr = at_phdr;
  359. sect_size = at_phent * at_phnum;
  360. }
  361. else if (arch_size == 32)
  362. {
  363. Elf32_External_Phdr phdr;
  364. int i;
  365. /* Search for requested PHDR. */
  366. for (i = 0; i < at_phnum; i++)
  367. {
  368. int p_type;
  369. if (target_read_memory (at_phdr + i * sizeof (phdr),
  370. (gdb_byte *)&phdr, sizeof (phdr)))
  371. return {};
  372. p_type = extract_unsigned_integer ((gdb_byte *) phdr.p_type,
  373. 4, byte_order);
  374. if (p_type == PT_PHDR)
  375. {
  376. pt_phdr_p = 1;
  377. pt_phdr = extract_unsigned_integer ((gdb_byte *) phdr.p_vaddr,
  378. 4, byte_order);
  379. }
  380. if (p_type == type)
  381. break;
  382. }
  383. if (i == at_phnum)
  384. return {};
  385. /* Retrieve address and size. */
  386. sect_addr = extract_unsigned_integer ((gdb_byte *)phdr.p_vaddr,
  387. 4, byte_order);
  388. sect_size = extract_unsigned_integer ((gdb_byte *)phdr.p_memsz,
  389. 4, byte_order);
  390. }
  391. else
  392. {
  393. Elf64_External_Phdr phdr;
  394. int i;
  395. /* Search for requested PHDR. */
  396. for (i = 0; i < at_phnum; i++)
  397. {
  398. int p_type;
  399. if (target_read_memory (at_phdr + i * sizeof (phdr),
  400. (gdb_byte *)&phdr, sizeof (phdr)))
  401. return {};
  402. p_type = extract_unsigned_integer ((gdb_byte *) phdr.p_type,
  403. 4, byte_order);
  404. if (p_type == PT_PHDR)
  405. {
  406. pt_phdr_p = 1;
  407. pt_phdr = extract_unsigned_integer ((gdb_byte *) phdr.p_vaddr,
  408. 8, byte_order);
  409. }
  410. if (p_type == type)
  411. break;
  412. }
  413. if (i == at_phnum)
  414. return {};
  415. /* Retrieve address and size. */
  416. sect_addr = extract_unsigned_integer ((gdb_byte *)phdr.p_vaddr,
  417. 8, byte_order);
  418. sect_size = extract_unsigned_integer ((gdb_byte *)phdr.p_memsz,
  419. 8, byte_order);
  420. }
  421. /* PT_PHDR is optional, but we really need it
  422. for PIE to make this work in general. */
  423. if (pt_phdr_p)
  424. {
  425. /* at_phdr is real address in memory. pt_phdr is what pheader says it is.
  426. Relocation offset is the difference between the two. */
  427. sect_addr = sect_addr + (at_phdr - pt_phdr);
  428. }
  429. /* Read in requested program header. */
  430. gdb::byte_vector buf (sect_size);
  431. if (target_read_memory (sect_addr, buf.data (), sect_size))
  432. return {};
  433. if (p_arch_size)
  434. *p_arch_size = arch_size;
  435. if (base_addr)
  436. *base_addr = sect_addr;
  437. return buf;
  438. }
  439. /* Return program interpreter string. */
  440. static gdb::optional<gdb::byte_vector>
  441. find_program_interpreter (void)
  442. {
  443. /* If we have a current exec_bfd, use its section table. */
  444. if (current_program_space->exec_bfd ()
  445. && (bfd_get_flavour (current_program_space->exec_bfd ())
  446. == bfd_target_elf_flavour))
  447. {
  448. struct bfd_section *interp_sect;
  449. interp_sect = bfd_get_section_by_name (current_program_space->exec_bfd (),
  450. ".interp");
  451. if (interp_sect != NULL)
  452. {
  453. int sect_size = bfd_section_size (interp_sect);
  454. gdb::byte_vector buf (sect_size);
  455. bfd_get_section_contents (current_program_space->exec_bfd (),
  456. interp_sect, buf.data (), 0, sect_size);
  457. return buf;
  458. }
  459. }
  460. /* If we didn't find it, use the target auxiliary vector. */
  461. return read_program_header (PT_INTERP, NULL, NULL);
  462. }
  463. /* Scan for DESIRED_DYNTAG in .dynamic section of the target's main executable,
  464. found by consulting the OS auxillary vector. If DESIRED_DYNTAG is found, 1
  465. is returned and the corresponding PTR is set. */
  466. static int
  467. scan_dyntag_auxv (const int desired_dyntag, CORE_ADDR *ptr,
  468. CORE_ADDR *ptr_addr)
  469. {
  470. enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  471. int arch_size, step;
  472. long current_dyntag;
  473. CORE_ADDR dyn_ptr;
  474. CORE_ADDR base_addr;
  475. /* Read in .dynamic section. */
  476. gdb::optional<gdb::byte_vector> ph_data
  477. = read_program_header (PT_DYNAMIC, &arch_size, &base_addr);
  478. if (!ph_data)
  479. return 0;
  480. /* Iterate over BUF and scan for DYNTAG. If found, set PTR and return. */
  481. step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
  482. : sizeof (Elf64_External_Dyn);
  483. for (gdb_byte *buf = ph_data->data (), *bufend = buf + ph_data->size ();
  484. buf < bufend; buf += step)
  485. {
  486. if (arch_size == 32)
  487. {
  488. Elf32_External_Dyn *dynp = (Elf32_External_Dyn *) buf;
  489. current_dyntag = extract_unsigned_integer ((gdb_byte *) dynp->d_tag,
  490. 4, byte_order);
  491. dyn_ptr = extract_unsigned_integer ((gdb_byte *) dynp->d_un.d_ptr,
  492. 4, byte_order);
  493. }
  494. else
  495. {
  496. Elf64_External_Dyn *dynp = (Elf64_External_Dyn *) buf;
  497. current_dyntag = extract_unsigned_integer ((gdb_byte *) dynp->d_tag,
  498. 8, byte_order);
  499. dyn_ptr = extract_unsigned_integer ((gdb_byte *) dynp->d_un.d_ptr,
  500. 8, byte_order);
  501. }
  502. if (current_dyntag == DT_NULL)
  503. break;
  504. if (current_dyntag == desired_dyntag)
  505. {
  506. if (ptr)
  507. *ptr = dyn_ptr;
  508. if (ptr_addr)
  509. *ptr_addr = base_addr + buf - ph_data->data ();
  510. return 1;
  511. }
  512. }
  513. return 0;
  514. }
  515. /* Locate the base address of dynamic linker structs for SVR4 elf
  516. targets.
  517. For SVR4 elf targets the address of the dynamic linker's runtime
  518. structure is contained within the dynamic info section in the
  519. executable file. The dynamic section is also mapped into the
  520. inferior address space. Because the runtime loader fills in the
  521. real address before starting the inferior, we have to read in the
  522. dynamic info section from the inferior address space.
  523. If there are any errors while trying to find the address, we
  524. silently return 0, otherwise the found address is returned. */
  525. static CORE_ADDR
  526. elf_locate_base (void)
  527. {
  528. struct bound_minimal_symbol msymbol;
  529. CORE_ADDR dyn_ptr, dyn_ptr_addr;
  530. /* Look for DT_MIPS_RLD_MAP first. MIPS executables use this
  531. instead of DT_DEBUG, although they sometimes contain an unused
  532. DT_DEBUG. */
  533. if (gdb_bfd_scan_elf_dyntag (DT_MIPS_RLD_MAP,
  534. current_program_space->exec_bfd (),
  535. &dyn_ptr, NULL)
  536. || scan_dyntag_auxv (DT_MIPS_RLD_MAP, &dyn_ptr, NULL))
  537. {
  538. struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
  539. gdb_byte *pbuf;
  540. int pbuf_size = TYPE_LENGTH (ptr_type);
  541. pbuf = (gdb_byte *) alloca (pbuf_size);
  542. /* DT_MIPS_RLD_MAP contains a pointer to the address
  543. of the dynamic link structure. */
  544. if (target_read_memory (dyn_ptr, pbuf, pbuf_size))
  545. return 0;
  546. return extract_typed_address (pbuf, ptr_type);
  547. }
  548. /* Then check DT_MIPS_RLD_MAP_REL. MIPS executables now use this form
  549. because of needing to support PIE. DT_MIPS_RLD_MAP will also exist
  550. in non-PIE. */
  551. if (gdb_bfd_scan_elf_dyntag (DT_MIPS_RLD_MAP_REL,
  552. current_program_space->exec_bfd (),
  553. &dyn_ptr, &dyn_ptr_addr)
  554. || scan_dyntag_auxv (DT_MIPS_RLD_MAP_REL, &dyn_ptr, &dyn_ptr_addr))
  555. {
  556. struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
  557. gdb_byte *pbuf;
  558. int pbuf_size = TYPE_LENGTH (ptr_type);
  559. pbuf = (gdb_byte *) alloca (pbuf_size);
  560. /* DT_MIPS_RLD_MAP_REL contains an offset from the address of the
  561. DT slot to the address of the dynamic link structure. */
  562. if (target_read_memory (dyn_ptr + dyn_ptr_addr, pbuf, pbuf_size))
  563. return 0;
  564. return extract_typed_address (pbuf, ptr_type);
  565. }
  566. /* Find DT_DEBUG. */
  567. if (gdb_bfd_scan_elf_dyntag (DT_DEBUG, current_program_space->exec_bfd (),
  568. &dyn_ptr, NULL)
  569. || scan_dyntag_auxv (DT_DEBUG, &dyn_ptr, NULL))
  570. return dyn_ptr;
  571. /* This may be a static executable. Look for the symbol
  572. conventionally named _r_debug, as a last resort. */
  573. msymbol = lookup_minimal_symbol ("_r_debug", NULL,
  574. current_program_space->symfile_object_file);
  575. if (msymbol.minsym != NULL)
  576. return BMSYMBOL_VALUE_ADDRESS (msymbol);
  577. /* DT_DEBUG entry not found. */
  578. return 0;
  579. }
  580. /* Locate the base address of dynamic linker structs.
  581. For both the SunOS and SVR4 shared library implementations, if the
  582. inferior executable has been linked dynamically, there is a single
  583. address somewhere in the inferior's data space which is the key to
  584. locating all of the dynamic linker's runtime structures. This
  585. address is the value of the debug base symbol. The job of this
  586. function is to find and return that address, or to return 0 if there
  587. is no such address (the executable is statically linked for example).
  588. For SunOS, the job is almost trivial, since the dynamic linker and
  589. all of it's structures are statically linked to the executable at
  590. link time. Thus the symbol for the address we are looking for has
  591. already been added to the minimal symbol table for the executable's
  592. objfile at the time the symbol file's symbols were read, and all we
  593. have to do is look it up there. Note that we explicitly do NOT want
  594. to find the copies in the shared library.
  595. The SVR4 version is a bit more complicated because the address
  596. is contained somewhere in the dynamic info section. We have to go
  597. to a lot more work to discover the address of the debug base symbol.
  598. Because of this complexity, we cache the value we find and return that
  599. value on subsequent invocations. Note there is no copy in the
  600. executable symbol tables. */
  601. static CORE_ADDR
  602. locate_base (struct svr4_info *info)
  603. {
  604. /* Check to see if we have a currently valid address, and if so, avoid
  605. doing all this work again and just return the cached address. If
  606. we have no cached address, try to locate it in the dynamic info
  607. section for ELF executables. There's no point in doing any of this
  608. though if we don't have some link map offsets to work with. */
  609. if (info->debug_base == 0 && svr4_have_link_map_offsets ())
  610. info->debug_base = elf_locate_base ();
  611. return info->debug_base;
  612. }
  613. /* Find the first element in the inferior's dynamic link map, and
  614. return its address in the inferior. Return zero if the address
  615. could not be determined.
  616. FIXME: Perhaps we should validate the info somehow, perhaps by
  617. checking r_version for a known version number, or r_state for
  618. RT_CONSISTENT. */
  619. static CORE_ADDR
  620. solib_svr4_r_map (struct svr4_info *info)
  621. {
  622. struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
  623. struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
  624. CORE_ADDR addr = 0;
  625. try
  626. {
  627. addr = read_memory_typed_address (info->debug_base + lmo->r_map_offset,
  628. ptr_type);
  629. }
  630. catch (const gdb_exception_error &ex)
  631. {
  632. exception_print (gdb_stderr, ex);
  633. }
  634. return addr;
  635. }
  636. /* Find r_brk from the inferior's debug base. */
  637. static CORE_ADDR
  638. solib_svr4_r_brk (struct svr4_info *info)
  639. {
  640. struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
  641. struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
  642. return read_memory_typed_address (info->debug_base + lmo->r_brk_offset,
  643. ptr_type);
  644. }
  645. /* Find the link map for the dynamic linker (if it is not in the
  646. normal list of loaded shared objects). */
  647. static CORE_ADDR
  648. solib_svr4_r_ldsomap (struct svr4_info *info)
  649. {
  650. struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
  651. struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
  652. enum bfd_endian byte_order = type_byte_order (ptr_type);
  653. ULONGEST version = 0;
  654. try
  655. {
  656. /* Check version, and return zero if `struct r_debug' doesn't have
  657. the r_ldsomap member. */
  658. version
  659. = read_memory_unsigned_integer (info->debug_base + lmo->r_version_offset,
  660. lmo->r_version_size, byte_order);
  661. }
  662. catch (const gdb_exception_error &ex)
  663. {
  664. exception_print (gdb_stderr, ex);
  665. }
  666. if (version < 2 || lmo->r_ldsomap_offset == -1)
  667. return 0;
  668. return read_memory_typed_address (info->debug_base + lmo->r_ldsomap_offset,
  669. ptr_type);
  670. }
  671. /* On Solaris systems with some versions of the dynamic linker,
  672. ld.so's l_name pointer points to the SONAME in the string table
  673. rather than into writable memory. So that GDB can find shared
  674. libraries when loading a core file generated by gcore, ensure that
  675. memory areas containing the l_name string are saved in the core
  676. file. */
  677. static int
  678. svr4_keep_data_in_core (CORE_ADDR vaddr, unsigned long size)
  679. {
  680. struct svr4_info *info;
  681. CORE_ADDR ldsomap;
  682. CORE_ADDR name_lm;
  683. info = get_svr4_info (current_program_space);
  684. info->debug_base = 0;
  685. locate_base (info);
  686. if (!info->debug_base)
  687. return 0;
  688. ldsomap = solib_svr4_r_ldsomap (info);
  689. if (!ldsomap)
  690. return 0;
  691. std::unique_ptr<lm_info_svr4> li = lm_info_read (ldsomap);
  692. name_lm = li != NULL ? li->l_name : 0;
  693. return (name_lm >= vaddr && name_lm < vaddr + size);
  694. }
  695. /* See solist.h. */
  696. static int
  697. open_symbol_file_object (int from_tty)
  698. {
  699. CORE_ADDR lm, l_name;
  700. struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
  701. struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
  702. int l_name_size = TYPE_LENGTH (ptr_type);
  703. gdb::byte_vector l_name_buf (l_name_size);
  704. struct svr4_info *info = get_svr4_info (current_program_space);
  705. symfile_add_flags add_flags = 0;
  706. if (from_tty)
  707. add_flags |= SYMFILE_VERBOSE;
  708. if (current_program_space->symfile_object_file)
  709. if (!query (_("Attempt to reload symbols from process? ")))
  710. return 0;
  711. /* Always locate the debug struct, in case it has moved. */
  712. info->debug_base = 0;
  713. if (locate_base (info) == 0)
  714. return 0; /* failed somehow... */
  715. /* First link map member should be the executable. */
  716. lm = solib_svr4_r_map (info);
  717. if (lm == 0)
  718. return 0; /* failed somehow... */
  719. /* Read address of name from target memory to GDB. */
  720. read_memory (lm + lmo->l_name_offset, l_name_buf.data (), l_name_size);
  721. /* Convert the address to host format. */
  722. l_name = extract_typed_address (l_name_buf.data (), ptr_type);
  723. if (l_name == 0)
  724. return 0; /* No filename. */
  725. /* Now fetch the filename from target memory. */
  726. gdb::unique_xmalloc_ptr<char> filename
  727. = target_read_string (l_name, SO_NAME_MAX_PATH_SIZE - 1);
  728. if (filename == nullptr)
  729. {
  730. warning (_("failed to read exec filename from attached file"));
  731. return 0;
  732. }
  733. /* Have a pathname: read the symbol file. */
  734. symbol_file_add_main (filename.get (), add_flags);
  735. return 1;
  736. }
  737. /* Data exchange structure for the XML parser as returned by
  738. svr4_current_sos_via_xfer_libraries. */
  739. struct svr4_library_list
  740. {
  741. struct so_list *head, **tailp;
  742. /* Inferior address of struct link_map used for the main executable. It is
  743. NULL if not known. */
  744. CORE_ADDR main_lm;
  745. };
  746. /* This module's 'free_objfile' observer. */
  747. static void
  748. svr4_free_objfile_observer (struct objfile *objfile)
  749. {
  750. probes_table_remove_objfile_probes (objfile);
  751. }
  752. /* Implementation for target_so_ops.free_so. */
  753. static void
  754. svr4_free_so (struct so_list *so)
  755. {
  756. lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
  757. delete li;
  758. }
  759. /* Implement target_so_ops.clear_so. */
  760. static void
  761. svr4_clear_so (struct so_list *so)
  762. {
  763. lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
  764. if (li != NULL)
  765. li->l_addr_p = 0;
  766. }
  767. /* Free so_list built so far (called via cleanup). */
  768. static void
  769. svr4_free_library_list (void *p_list)
  770. {
  771. struct so_list *list = *(struct so_list **) p_list;
  772. while (list != NULL)
  773. {
  774. struct so_list *next = list->next;
  775. free_so (list);
  776. list = next;
  777. }
  778. }
  779. /* Copy library list. */
  780. static struct so_list *
  781. svr4_copy_library_list (struct so_list *src)
  782. {
  783. struct so_list *dst = NULL;
  784. struct so_list **link = &dst;
  785. while (src != NULL)
  786. {
  787. struct so_list *newobj;
  788. newobj = XNEW (struct so_list);
  789. memcpy (newobj, src, sizeof (struct so_list));
  790. lm_info_svr4 *src_li = (lm_info_svr4 *) src->lm_info;
  791. newobj->lm_info = new lm_info_svr4 (*src_li);
  792. newobj->next = NULL;
  793. *link = newobj;
  794. link = &newobj->next;
  795. src = src->next;
  796. }
  797. return dst;
  798. }
  799. #ifdef HAVE_LIBEXPAT
  800. #include "xml-support.h"
  801. /* Handle the start of a <library> element. Note: new elements are added
  802. at the tail of the list, keeping the list in order. */
  803. static void
  804. library_list_start_library (struct gdb_xml_parser *parser,
  805. const struct gdb_xml_element *element,
  806. void *user_data,
  807. std::vector<gdb_xml_value> &attributes)
  808. {
  809. struct svr4_library_list *list = (struct svr4_library_list *) user_data;
  810. const char *name
  811. = (const char *) xml_find_attribute (attributes, "name")->value.get ();
  812. ULONGEST *lmp
  813. = (ULONGEST *) xml_find_attribute (attributes, "lm")->value.get ();
  814. ULONGEST *l_addrp
  815. = (ULONGEST *) xml_find_attribute (attributes, "l_addr")->value.get ();
  816. ULONGEST *l_ldp
  817. = (ULONGEST *) xml_find_attribute (attributes, "l_ld")->value.get ();
  818. struct so_list *new_elem;
  819. new_elem = XCNEW (struct so_list);
  820. lm_info_svr4 *li = new lm_info_svr4;
  821. new_elem->lm_info = li;
  822. li->lm_addr = *lmp;
  823. li->l_addr_inferior = *l_addrp;
  824. li->l_ld = *l_ldp;
  825. strncpy (new_elem->so_name, name, sizeof (new_elem->so_name) - 1);
  826. new_elem->so_name[sizeof (new_elem->so_name) - 1] = 0;
  827. strcpy (new_elem->so_original_name, new_elem->so_name);
  828. *list->tailp = new_elem;
  829. list->tailp = &new_elem->next;
  830. }
  831. /* Handle the start of a <library-list-svr4> element. */
  832. static void
  833. svr4_library_list_start_list (struct gdb_xml_parser *parser,
  834. const struct gdb_xml_element *element,
  835. void *user_data,
  836. std::vector<gdb_xml_value> &attributes)
  837. {
  838. struct svr4_library_list *list = (struct svr4_library_list *) user_data;
  839. const char *version
  840. = (const char *) xml_find_attribute (attributes, "version")->value.get ();
  841. struct gdb_xml_value *main_lm = xml_find_attribute (attributes, "main-lm");
  842. if (strcmp (version, "1.0") != 0)
  843. gdb_xml_error (parser,
  844. _("SVR4 Library list has unsupported version \"%s\""),
  845. version);
  846. if (main_lm)
  847. list->main_lm = *(ULONGEST *) main_lm->value.get ();
  848. }
  849. /* The allowed elements and attributes for an XML library list.
  850. The root element is a <library-list>. */
  851. static const struct gdb_xml_attribute svr4_library_attributes[] =
  852. {
  853. { "name", GDB_XML_AF_NONE, NULL, NULL },
  854. { "lm", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
  855. { "l_addr", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
  856. { "l_ld", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
  857. { NULL, GDB_XML_AF_NONE, NULL, NULL }
  858. };
  859. static const struct gdb_xml_element svr4_library_list_children[] =
  860. {
  861. {
  862. "library", svr4_library_attributes, NULL,
  863. GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
  864. library_list_start_library, NULL
  865. },
  866. { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  867. };
  868. static const struct gdb_xml_attribute svr4_library_list_attributes[] =
  869. {
  870. { "version", GDB_XML_AF_NONE, NULL, NULL },
  871. { "main-lm", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
  872. { NULL, GDB_XML_AF_NONE, NULL, NULL }
  873. };
  874. static const struct gdb_xml_element svr4_library_list_elements[] =
  875. {
  876. { "library-list-svr4", svr4_library_list_attributes, svr4_library_list_children,
  877. GDB_XML_EF_NONE, svr4_library_list_start_list, NULL },
  878. { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  879. };
  880. /* Parse qXfer:libraries:read packet into *SO_LIST_RETURN. Return 1 if
  881. Return 0 if packet not supported, *SO_LIST_RETURN is not modified in such
  882. case. Return 1 if *SO_LIST_RETURN contains the library list, it may be
  883. empty, caller is responsible for freeing all its entries. */
  884. static int
  885. svr4_parse_libraries (const char *document, struct svr4_library_list *list)
  886. {
  887. auto cleanup = make_scope_exit ([&] ()
  888. {
  889. svr4_free_library_list (&list->head);
  890. });
  891. memset (list, 0, sizeof (*list));
  892. list->tailp = &list->head;
  893. if (gdb_xml_parse_quick (_("target library list"), "library-list-svr4.dtd",
  894. svr4_library_list_elements, document, list) == 0)
  895. {
  896. /* Parsed successfully, keep the result. */
  897. cleanup.release ();
  898. return 1;
  899. }
  900. return 0;
  901. }
  902. /* Attempt to get so_list from target via qXfer:libraries-svr4:read packet.
  903. Return 0 if packet not supported, *SO_LIST_RETURN is not modified in such
  904. case. Return 1 if *SO_LIST_RETURN contains the library list, it may be
  905. empty, caller is responsible for freeing all its entries.
  906. Note that ANNEX must be NULL if the remote does not explicitly allow
  907. qXfer:libraries-svr4:read packets with non-empty annexes. Support for
  908. this can be checked using target_augmented_libraries_svr4_read (). */
  909. static int
  910. svr4_current_sos_via_xfer_libraries (struct svr4_library_list *list,
  911. const char *annex)
  912. {
  913. gdb_assert (annex == NULL || target_augmented_libraries_svr4_read ());
  914. /* Fetch the list of shared libraries. */
  915. gdb::optional<gdb::char_vector> svr4_library_document
  916. = target_read_stralloc (current_inferior ()->top_target (),
  917. TARGET_OBJECT_LIBRARIES_SVR4,
  918. annex);
  919. if (!svr4_library_document)
  920. return 0;
  921. return svr4_parse_libraries (svr4_library_document->data (), list);
  922. }
  923. #else
  924. static int
  925. svr4_current_sos_via_xfer_libraries (struct svr4_library_list *list,
  926. const char *annex)
  927. {
  928. return 0;
  929. }
  930. #endif
  931. /* If no shared library information is available from the dynamic
  932. linker, build a fallback list from other sources. */
  933. static struct so_list *
  934. svr4_default_sos (svr4_info *info)
  935. {
  936. struct so_list *newobj;
  937. if (!info->debug_loader_offset_p)
  938. return NULL;
  939. newobj = XCNEW (struct so_list);
  940. lm_info_svr4 *li = new lm_info_svr4;
  941. newobj->lm_info = li;
  942. /* Nothing will ever check the other fields if we set l_addr_p. */
  943. li->l_addr = info->debug_loader_offset;
  944. li->l_addr_p = 1;
  945. strncpy (newobj->so_name, info->debug_loader_name, SO_NAME_MAX_PATH_SIZE - 1);
  946. newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
  947. strcpy (newobj->so_original_name, newobj->so_name);
  948. return newobj;
  949. }
  950. /* Read the whole inferior libraries chain starting at address LM.
  951. Expect the first entry in the chain's previous entry to be PREV_LM.
  952. Add the entries to the tail referenced by LINK_PTR_PTR. Ignore the
  953. first entry if IGNORE_FIRST and set global MAIN_LM_ADDR according
  954. to it. Returns nonzero upon success. If zero is returned the
  955. entries stored to LINK_PTR_PTR are still valid although they may
  956. represent only part of the inferior library list. */
  957. static int
  958. svr4_read_so_list (svr4_info *info, CORE_ADDR lm, CORE_ADDR prev_lm,
  959. struct so_list ***link_ptr_ptr, int ignore_first)
  960. {
  961. CORE_ADDR first_l_name = 0;
  962. CORE_ADDR next_lm;
  963. for (; lm != 0; prev_lm = lm, lm = next_lm)
  964. {
  965. so_list_up newobj (XCNEW (struct so_list));
  966. lm_info_svr4 *li = lm_info_read (lm).release ();
  967. newobj->lm_info = li;
  968. if (li == NULL)
  969. return 0;
  970. next_lm = li->l_next;
  971. if (li->l_prev != prev_lm)
  972. {
  973. warning (_("Corrupted shared library list: %s != %s"),
  974. paddress (target_gdbarch (), prev_lm),
  975. paddress (target_gdbarch (), li->l_prev));
  976. return 0;
  977. }
  978. /* For SVR4 versions, the first entry in the link map is for the
  979. inferior executable, so we must ignore it. For some versions of
  980. SVR4, it has no name. For others (Solaris 2.3 for example), it
  981. does have a name, so we can no longer use a missing name to
  982. decide when to ignore it. */
  983. if (ignore_first && li->l_prev == 0)
  984. {
  985. first_l_name = li->l_name;
  986. info->main_lm_addr = li->lm_addr;
  987. continue;
  988. }
  989. /* Extract this shared object's name. */
  990. gdb::unique_xmalloc_ptr<char> buffer
  991. = target_read_string (li->l_name, SO_NAME_MAX_PATH_SIZE - 1);
  992. if (buffer == nullptr)
  993. {
  994. /* If this entry's l_name address matches that of the
  995. inferior executable, then this is not a normal shared
  996. object, but (most likely) a vDSO. In this case, silently
  997. skip it; otherwise emit a warning. */
  998. if (first_l_name == 0 || li->l_name != first_l_name)
  999. warning (_("Can't read pathname for load map."));
  1000. continue;
  1001. }
  1002. strncpy (newobj->so_name, buffer.get (), SO_NAME_MAX_PATH_SIZE - 1);
  1003. newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
  1004. strcpy (newobj->so_original_name, newobj->so_name);
  1005. /* If this entry has no name, or its name matches the name
  1006. for the main executable, don't include it in the list. */
  1007. if (! newobj->so_name[0] || match_main (newobj->so_name))
  1008. continue;
  1009. newobj->next = 0;
  1010. /* Don't free it now. */
  1011. **link_ptr_ptr = newobj.release ();
  1012. *link_ptr_ptr = &(**link_ptr_ptr)->next;
  1013. }
  1014. return 1;
  1015. }
  1016. /* Read the full list of currently loaded shared objects directly
  1017. from the inferior, without referring to any libraries read and
  1018. stored by the probes interface. Handle special cases relating
  1019. to the first elements of the list. */
  1020. static struct so_list *
  1021. svr4_current_sos_direct (struct svr4_info *info)
  1022. {
  1023. CORE_ADDR lm;
  1024. struct so_list *head = NULL;
  1025. struct so_list **link_ptr = &head;
  1026. int ignore_first;
  1027. struct svr4_library_list library_list;
  1028. /* Fall back to manual examination of the target if the packet is not
  1029. supported or gdbserver failed to find DT_DEBUG. gdb.server/solib-list.exp
  1030. tests a case where gdbserver cannot find the shared libraries list while
  1031. GDB itself is able to find it via SYMFILE_OBJFILE.
  1032. Unfortunately statically linked inferiors will also fall back through this
  1033. suboptimal code path. */
  1034. info->using_xfer = svr4_current_sos_via_xfer_libraries (&library_list,
  1035. NULL);
  1036. if (info->using_xfer)
  1037. {
  1038. if (library_list.main_lm)
  1039. info->main_lm_addr = library_list.main_lm;
  1040. return library_list.head ? library_list.head : svr4_default_sos (info);
  1041. }
  1042. /* Always locate the debug struct, in case it has moved. */
  1043. info->debug_base = 0;
  1044. locate_base (info);
  1045. /* If we can't find the dynamic linker's base structure, this
  1046. must not be a dynamically linked executable. Hmm. */
  1047. if (! info->debug_base)
  1048. return svr4_default_sos (info);
  1049. /* Assume that everything is a library if the dynamic loader was loaded
  1050. late by a static executable. */
  1051. if (current_program_space->exec_bfd ()
  1052. && bfd_get_section_by_name (current_program_space->exec_bfd (),
  1053. ".dynamic") == NULL)
  1054. ignore_first = 0;
  1055. else
  1056. ignore_first = 1;
  1057. auto cleanup = make_scope_exit ([&] ()
  1058. {
  1059. svr4_free_library_list (&head);
  1060. });
  1061. /* Walk the inferior's link map list, and build our list of
  1062. `struct so_list' nodes. */
  1063. lm = solib_svr4_r_map (info);
  1064. if (lm)
  1065. svr4_read_so_list (info, lm, 0, &link_ptr, ignore_first);
  1066. /* On Solaris, the dynamic linker is not in the normal list of
  1067. shared objects, so make sure we pick it up too. Having
  1068. symbol information for the dynamic linker is quite crucial
  1069. for skipping dynamic linker resolver code. */
  1070. lm = solib_svr4_r_ldsomap (info);
  1071. if (lm)
  1072. svr4_read_so_list (info, lm, 0, &link_ptr, 0);
  1073. cleanup.release ();
  1074. if (head == NULL)
  1075. return svr4_default_sos (info);
  1076. return head;
  1077. }
  1078. /* Implement the main part of the "current_sos" target_so_ops
  1079. method. */
  1080. static struct so_list *
  1081. svr4_current_sos_1 (svr4_info *info)
  1082. {
  1083. /* If the solib list has been read and stored by the probes
  1084. interface then we return a copy of the stored list. */
  1085. if (info->solib_list != NULL)
  1086. return svr4_copy_library_list (info->solib_list);
  1087. /* Otherwise obtain the solib list directly from the inferior. */
  1088. return svr4_current_sos_direct (info);
  1089. }
  1090. /* Implement the "current_sos" target_so_ops method. */
  1091. static struct so_list *
  1092. svr4_current_sos (void)
  1093. {
  1094. svr4_info *info = get_svr4_info (current_program_space);
  1095. struct so_list *so_head = svr4_current_sos_1 (info);
  1096. struct mem_range vsyscall_range;
  1097. /* Filter out the vDSO module, if present. Its symbol file would
  1098. not be found on disk. The vDSO/vsyscall's OBJFILE is instead
  1099. managed by symfile-mem.c:add_vsyscall_page. */
  1100. if (gdbarch_vsyscall_range (target_gdbarch (), &vsyscall_range)
  1101. && vsyscall_range.length != 0)
  1102. {
  1103. struct so_list **sop;
  1104. sop = &so_head;
  1105. while (*sop != NULL)
  1106. {
  1107. struct so_list *so = *sop;
  1108. /* We can't simply match the vDSO by starting address alone,
  1109. because lm_info->l_addr_inferior (and also l_addr) do not
  1110. necessarily represent the real starting address of the
  1111. ELF if the vDSO's ELF itself is "prelinked". The l_ld
  1112. field (the ".dynamic" section of the shared object)
  1113. always points at the absolute/resolved address though.
  1114. So check whether that address is inside the vDSO's
  1115. mapping instead.
  1116. E.g., on Linux 3.16 (x86_64) the vDSO is a regular
  1117. 0-based ELF, and we see:
  1118. (gdb) info auxv
  1119. 33 AT_SYSINFO_EHDR System-supplied DSO's ELF header 0x7ffff7ffb000
  1120. (gdb) p/x *_r_debug.r_map.l_next
  1121. $1 = {l_addr = 0x7ffff7ffb000, ..., l_ld = 0x7ffff7ffb318, ...}
  1122. And on Linux 2.6.32 (x86_64) we see:
  1123. (gdb) info auxv
  1124. 33 AT_SYSINFO_EHDR System-supplied DSO's ELF header 0x7ffff7ffe000
  1125. (gdb) p/x *_r_debug.r_map.l_next
  1126. $5 = {l_addr = 0x7ffff88fe000, ..., l_ld = 0x7ffff7ffe580, ... }
  1127. Dumping that vDSO shows:
  1128. (gdb) info proc mappings
  1129. 0x7ffff7ffe000 0x7ffff7fff000 0x1000 0 [vdso]
  1130. (gdb) dump memory vdso.bin 0x7ffff7ffe000 0x7ffff7fff000
  1131. # readelf -Wa vdso.bin
  1132. [...]
  1133. Entry point address: 0xffffffffff700700
  1134. [...]
  1135. Section Headers:
  1136. [Nr] Name Type Address Off Size
  1137. [ 0] NULL 0000000000000000 000000 000000
  1138. [ 1] .hash HASH ffffffffff700120 000120 000038
  1139. [ 2] .dynsym DYNSYM ffffffffff700158 000158 0000d8
  1140. [...]
  1141. [ 9] .dynamic DYNAMIC ffffffffff700580 000580 0000f0
  1142. */
  1143. lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
  1144. if (address_in_mem_range (li->l_ld, &vsyscall_range))
  1145. {
  1146. *sop = so->next;
  1147. free_so (so);
  1148. break;
  1149. }
  1150. sop = &so->next;
  1151. }
  1152. }
  1153. return so_head;
  1154. }
  1155. /* Get the address of the link_map for a given OBJFILE. */
  1156. CORE_ADDR
  1157. svr4_fetch_objfile_link_map (struct objfile *objfile)
  1158. {
  1159. struct svr4_info *info = get_svr4_info (objfile->pspace);
  1160. /* Cause svr4_current_sos() to be run if it hasn't been already. */
  1161. if (info->main_lm_addr == 0)
  1162. solib_add (NULL, 0, auto_solib_add);
  1163. /* svr4_current_sos() will set main_lm_addr for the main executable. */
  1164. if (objfile == current_program_space->symfile_object_file)
  1165. return info->main_lm_addr;
  1166. /* The other link map addresses may be found by examining the list
  1167. of shared libraries. */
  1168. for (struct so_list *so : current_program_space->solibs ())
  1169. if (so->objfile == objfile)
  1170. {
  1171. lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
  1172. return li->lm_addr;
  1173. }
  1174. /* Not found! */
  1175. return 0;
  1176. }
  1177. /* On some systems, the only way to recognize the link map entry for
  1178. the main executable file is by looking at its name. Return
  1179. non-zero iff SONAME matches one of the known main executable names. */
  1180. static int
  1181. match_main (const char *soname)
  1182. {
  1183. const char * const *mainp;
  1184. for (mainp = main_name_list; *mainp != NULL; mainp++)
  1185. {
  1186. if (strcmp (soname, *mainp) == 0)
  1187. return (1);
  1188. }
  1189. return (0);
  1190. }
  1191. /* Return 1 if PC lies in the dynamic symbol resolution code of the
  1192. SVR4 run time loader. */
  1193. int
  1194. svr4_in_dynsym_resolve_code (CORE_ADDR pc)
  1195. {
  1196. struct svr4_info *info = get_svr4_info (current_program_space);
  1197. return ((pc >= info->interp_text_sect_low
  1198. && pc < info->interp_text_sect_high)
  1199. || (pc >= info->interp_plt_sect_low
  1200. && pc < info->interp_plt_sect_high)
  1201. || in_plt_section (pc)
  1202. || in_gnu_ifunc_stub (pc));
  1203. }
  1204. /* Given an executable's ABFD and target, compute the entry-point
  1205. address. */
  1206. static CORE_ADDR
  1207. exec_entry_point (struct bfd *abfd, struct target_ops *targ)
  1208. {
  1209. CORE_ADDR addr;
  1210. /* KevinB wrote ... for most targets, the address returned by
  1211. bfd_get_start_address() is the entry point for the start
  1212. function. But, for some targets, bfd_get_start_address() returns
  1213. the address of a function descriptor from which the entry point
  1214. address may be extracted. This address is extracted by
  1215. gdbarch_convert_from_func_ptr_addr(). The method
  1216. gdbarch_convert_from_func_ptr_addr() is the merely the identify
  1217. function for targets which don't use function descriptors. */
  1218. addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
  1219. bfd_get_start_address (abfd),
  1220. targ);
  1221. return gdbarch_addr_bits_remove (target_gdbarch (), addr);
  1222. }
  1223. /* A probe and its associated action. */
  1224. struct probe_and_action
  1225. {
  1226. /* The probe. */
  1227. probe *prob;
  1228. /* The relocated address of the probe. */
  1229. CORE_ADDR address;
  1230. /* The action. */
  1231. enum probe_action action;
  1232. /* The objfile where this probe was found. */
  1233. struct objfile *objfile;
  1234. };
  1235. /* Returns a hash code for the probe_and_action referenced by p. */
  1236. static hashval_t
  1237. hash_probe_and_action (const void *p)
  1238. {
  1239. const struct probe_and_action *pa = (const struct probe_and_action *) p;
  1240. return (hashval_t) pa->address;
  1241. }
  1242. /* Returns non-zero if the probe_and_actions referenced by p1 and p2
  1243. are equal. */
  1244. static int
  1245. equal_probe_and_action (const void *p1, const void *p2)
  1246. {
  1247. const struct probe_and_action *pa1 = (const struct probe_and_action *) p1;
  1248. const struct probe_and_action *pa2 = (const struct probe_and_action *) p2;
  1249. return pa1->address == pa2->address;
  1250. }
  1251. /* Traversal function for probes_table_remove_objfile_probes. */
  1252. static int
  1253. probes_table_htab_remove_objfile_probes (void **slot, void *info)
  1254. {
  1255. probe_and_action *pa = (probe_and_action *) *slot;
  1256. struct objfile *objfile = (struct objfile *) info;
  1257. if (pa->objfile == objfile)
  1258. htab_clear_slot (get_svr4_info (objfile->pspace)->probes_table.get (),
  1259. slot);
  1260. return 1;
  1261. }
  1262. /* Remove all probes that belong to OBJFILE from the probes table. */
  1263. static void
  1264. probes_table_remove_objfile_probes (struct objfile *objfile)
  1265. {
  1266. svr4_info *info = get_svr4_info (objfile->pspace);
  1267. if (info->probes_table != nullptr)
  1268. htab_traverse_noresize (info->probes_table.get (),
  1269. probes_table_htab_remove_objfile_probes, objfile);
  1270. }
  1271. /* Register a solib event probe and its associated action in the
  1272. probes table. */
  1273. static void
  1274. register_solib_event_probe (svr4_info *info, struct objfile *objfile,
  1275. probe *prob, CORE_ADDR address,
  1276. enum probe_action action)
  1277. {
  1278. struct probe_and_action lookup, *pa;
  1279. void **slot;
  1280. /* Create the probes table, if necessary. */
  1281. if (info->probes_table == NULL)
  1282. info->probes_table.reset (htab_create_alloc (1, hash_probe_and_action,
  1283. equal_probe_and_action,
  1284. xfree, xcalloc, xfree));
  1285. lookup.address = address;
  1286. slot = htab_find_slot (info->probes_table.get (), &lookup, INSERT);
  1287. gdb_assert (*slot == HTAB_EMPTY_ENTRY);
  1288. pa = XCNEW (struct probe_and_action);
  1289. pa->prob = prob;
  1290. pa->address = address;
  1291. pa->action = action;
  1292. pa->objfile = objfile;
  1293. *slot = pa;
  1294. }
  1295. /* Get the solib event probe at the specified location, and the
  1296. action associated with it. Returns NULL if no solib event probe
  1297. was found. */
  1298. static struct probe_and_action *
  1299. solib_event_probe_at (struct svr4_info *info, CORE_ADDR address)
  1300. {
  1301. struct probe_and_action lookup;
  1302. void **slot;
  1303. lookup.address = address;
  1304. slot = htab_find_slot (info->probes_table.get (), &lookup, NO_INSERT);
  1305. if (slot == NULL)
  1306. return NULL;
  1307. return (struct probe_and_action *) *slot;
  1308. }
  1309. /* Decide what action to take when the specified solib event probe is
  1310. hit. */
  1311. static enum probe_action
  1312. solib_event_probe_action (struct probe_and_action *pa)
  1313. {
  1314. enum probe_action action;
  1315. unsigned probe_argc = 0;
  1316. struct frame_info *frame = get_current_frame ();
  1317. action = pa->action;
  1318. if (action == DO_NOTHING || action == PROBES_INTERFACE_FAILED)
  1319. return action;
  1320. gdb_assert (action == FULL_RELOAD || action == UPDATE_OR_RELOAD);
  1321. /* Check that an appropriate number of arguments has been supplied.
  1322. We expect:
  1323. arg0: Lmid_t lmid (mandatory)
  1324. arg1: struct r_debug *debug_base (mandatory)
  1325. arg2: struct link_map *new (optional, for incremental updates) */
  1326. try
  1327. {
  1328. probe_argc = pa->prob->get_argument_count (get_frame_arch (frame));
  1329. }
  1330. catch (const gdb_exception_error &ex)
  1331. {
  1332. exception_print (gdb_stderr, ex);
  1333. probe_argc = 0;
  1334. }
  1335. /* If get_argument_count throws an exception, probe_argc will be set
  1336. to zero. However, if pa->prob does not have arguments, then
  1337. get_argument_count will succeed but probe_argc will also be zero.
  1338. Both cases happen because of different things, but they are
  1339. treated equally here: action will be set to
  1340. PROBES_INTERFACE_FAILED. */
  1341. if (probe_argc == 2)
  1342. action = FULL_RELOAD;
  1343. else if (probe_argc < 2)
  1344. action = PROBES_INTERFACE_FAILED;
  1345. return action;
  1346. }
  1347. /* Populate the shared object list by reading the entire list of
  1348. shared objects from the inferior. Handle special cases relating
  1349. to the first elements of the list. Returns nonzero on success. */
  1350. static int
  1351. solist_update_full (struct svr4_info *info)
  1352. {
  1353. free_solib_list (info);
  1354. info->solib_list = svr4_current_sos_direct (info);
  1355. return 1;
  1356. }
  1357. /* Update the shared object list starting from the link-map entry
  1358. passed by the linker in the probe's third argument. Returns
  1359. nonzero if the list was successfully updated, or zero to indicate
  1360. failure. */
  1361. static int
  1362. solist_update_incremental (struct svr4_info *info, CORE_ADDR lm)
  1363. {
  1364. struct so_list *tail;
  1365. CORE_ADDR prev_lm;
  1366. /* svr4_current_sos_direct contains logic to handle a number of
  1367. special cases relating to the first elements of the list. To
  1368. avoid duplicating this logic we defer to solist_update_full
  1369. if the list is empty. */
  1370. if (info->solib_list == NULL)
  1371. return 0;
  1372. /* Fall back to a full update if we are using a remote target
  1373. that does not support incremental transfers. */
  1374. if (info->using_xfer && !target_augmented_libraries_svr4_read ())
  1375. return 0;
  1376. /* Walk to the end of the list. */
  1377. for (tail = info->solib_list; tail->next != NULL; tail = tail->next)
  1378. /* Nothing. */;
  1379. lm_info_svr4 *li = (lm_info_svr4 *) tail->lm_info;
  1380. prev_lm = li->lm_addr;
  1381. /* Read the new objects. */
  1382. if (info->using_xfer)
  1383. {
  1384. struct svr4_library_list library_list;
  1385. char annex[64];
  1386. xsnprintf (annex, sizeof (annex), "start=%s;prev=%s",
  1387. phex_nz (lm, sizeof (lm)),
  1388. phex_nz (prev_lm, sizeof (prev_lm)));
  1389. if (!svr4_current_sos_via_xfer_libraries (&library_list, annex))
  1390. return 0;
  1391. tail->next = library_list.head;
  1392. }
  1393. else
  1394. {
  1395. struct so_list **link = &tail->next;
  1396. /* IGNORE_FIRST may safely be set to zero here because the
  1397. above check and deferral to solist_update_full ensures
  1398. that this call to svr4_read_so_list will never see the
  1399. first element. */
  1400. if (!svr4_read_so_list (info, lm, prev_lm, &link, 0))
  1401. return 0;
  1402. }
  1403. return 1;
  1404. }
  1405. /* Disable the probes-based linker interface and revert to the
  1406. original interface. We don't reset the breakpoints as the
  1407. ones set up for the probes-based interface are adequate. */
  1408. static void
  1409. disable_probes_interface (svr4_info *info)
  1410. {
  1411. warning (_("Probes-based dynamic linker interface failed.\n"
  1412. "Reverting to original interface."));
  1413. free_probes_table (info);
  1414. free_solib_list (info);
  1415. }
  1416. /* Update the solib list as appropriate when using the
  1417. probes-based linker interface. Do nothing if using the
  1418. standard interface. */
  1419. static void
  1420. svr4_handle_solib_event (void)
  1421. {
  1422. struct svr4_info *info = get_svr4_info (current_program_space);
  1423. struct probe_and_action *pa;
  1424. enum probe_action action;
  1425. struct value *val = NULL;
  1426. CORE_ADDR pc, debug_base, lm = 0;
  1427. struct frame_info *frame = get_current_frame ();
  1428. /* Do nothing if not using the probes interface. */
  1429. if (info->probes_table == NULL)
  1430. return;
  1431. /* If anything goes wrong we revert to the original linker
  1432. interface. */
  1433. auto cleanup = make_scope_exit ([info] ()
  1434. {
  1435. disable_probes_interface (info);
  1436. });
  1437. pc = regcache_read_pc (get_current_regcache ());
  1438. pa = solib_event_probe_at (info, pc);
  1439. if (pa == NULL)
  1440. return;
  1441. action = solib_event_probe_action (pa);
  1442. if (action == PROBES_INTERFACE_FAILED)
  1443. return;
  1444. if (action == DO_NOTHING)
  1445. {
  1446. cleanup.release ();
  1447. return;
  1448. }
  1449. /* evaluate_argument looks up symbols in the dynamic linker
  1450. using find_pc_section. find_pc_section is accelerated by a cache
  1451. called the section map. The section map is invalidated every
  1452. time a shared library is loaded or unloaded, and if the inferior
  1453. is generating a lot of shared library events then the section map
  1454. will be updated every time svr4_handle_solib_event is called.
  1455. We called find_pc_section in svr4_create_solib_event_breakpoints,
  1456. so we can guarantee that the dynamic linker's sections are in the
  1457. section map. We can therefore inhibit section map updates across
  1458. these calls to evaluate_argument and save a lot of time. */
  1459. {
  1460. scoped_restore inhibit_updates
  1461. = inhibit_section_map_updates (current_program_space);
  1462. try
  1463. {
  1464. val = pa->prob->evaluate_argument (1, frame);
  1465. }
  1466. catch (const gdb_exception_error &ex)
  1467. {
  1468. exception_print (gdb_stderr, ex);
  1469. val = NULL;
  1470. }
  1471. if (val == NULL)
  1472. return;
  1473. debug_base = value_as_address (val);
  1474. if (debug_base == 0)
  1475. return;
  1476. /* Always locate the debug struct, in case it moved. */
  1477. info->debug_base = 0;
  1478. if (locate_base (info) == 0)
  1479. {
  1480. /* It's possible for the reloc_complete probe to be triggered before
  1481. the linker has set the DT_DEBUG pointer (for example, when the
  1482. linker has finished relocating an LD_AUDIT library or its
  1483. dependencies). Since we can't yet handle libraries from other link
  1484. namespaces, we don't lose anything by ignoring them here. */
  1485. struct value *link_map_id_val;
  1486. try
  1487. {
  1488. link_map_id_val = pa->prob->evaluate_argument (0, frame);
  1489. }
  1490. catch (const gdb_exception_error)
  1491. {
  1492. link_map_id_val = NULL;
  1493. }
  1494. /* glibc and illumos' libc both define LM_ID_BASE as zero. */
  1495. if (link_map_id_val != NULL && value_as_long (link_map_id_val) != 0)
  1496. action = DO_NOTHING;
  1497. else
  1498. return;
  1499. }
  1500. /* GDB does not currently support libraries loaded via dlmopen
  1501. into namespaces other than the initial one. We must ignore
  1502. any namespace other than the initial namespace here until
  1503. support for this is added to GDB. */
  1504. if (debug_base != info->debug_base)
  1505. action = DO_NOTHING;
  1506. if (action == UPDATE_OR_RELOAD)
  1507. {
  1508. try
  1509. {
  1510. val = pa->prob->evaluate_argument (2, frame);
  1511. }
  1512. catch (const gdb_exception_error &ex)
  1513. {
  1514. exception_print (gdb_stderr, ex);
  1515. return;
  1516. }
  1517. if (val != NULL)
  1518. lm = value_as_address (val);
  1519. if (lm == 0)
  1520. action = FULL_RELOAD;
  1521. }
  1522. /* Resume section map updates. Closing the scope is
  1523. sufficient. */
  1524. }
  1525. if (action == UPDATE_OR_RELOAD)
  1526. {
  1527. if (!solist_update_incremental (info, lm))
  1528. action = FULL_RELOAD;
  1529. }
  1530. if (action == FULL_RELOAD)
  1531. {
  1532. if (!solist_update_full (info))
  1533. return;
  1534. }
  1535. cleanup.release ();
  1536. }
  1537. /* Helper function for svr4_update_solib_event_breakpoints. */
  1538. static bool
  1539. svr4_update_solib_event_breakpoint (struct breakpoint *b)
  1540. {
  1541. if (b->type != bp_shlib_event)
  1542. {
  1543. /* Continue iterating. */
  1544. return false;
  1545. }
  1546. for (bp_location *loc : b->locations ())
  1547. {
  1548. struct svr4_info *info;
  1549. struct probe_and_action *pa;
  1550. info = solib_svr4_pspace_data.get (loc->pspace);
  1551. if (info == NULL || info->probes_table == NULL)
  1552. continue;
  1553. pa = solib_event_probe_at (info, loc->address);
  1554. if (pa == NULL)
  1555. continue;
  1556. if (pa->action == DO_NOTHING)
  1557. {
  1558. if (b->enable_state == bp_disabled && stop_on_solib_events)
  1559. enable_breakpoint (b);
  1560. else if (b->enable_state == bp_enabled && !stop_on_solib_events)
  1561. disable_breakpoint (b);
  1562. }
  1563. break;
  1564. }
  1565. /* Continue iterating. */
  1566. return false;
  1567. }
  1568. /* Enable or disable optional solib event breakpoints as appropriate.
  1569. Called whenever stop_on_solib_events is changed. */
  1570. static void
  1571. svr4_update_solib_event_breakpoints (void)
  1572. {
  1573. for (breakpoint *bp : all_breakpoints_safe ())
  1574. svr4_update_solib_event_breakpoint (bp);
  1575. }
  1576. /* Create and register solib event breakpoints. PROBES is an array
  1577. of NUM_PROBES elements, each of which is vector of probes. A
  1578. solib event breakpoint will be created and registered for each
  1579. probe. */
  1580. static void
  1581. svr4_create_probe_breakpoints (svr4_info *info, struct gdbarch *gdbarch,
  1582. const std::vector<probe *> *probes,
  1583. struct objfile *objfile)
  1584. {
  1585. for (int i = 0; i < NUM_PROBES; i++)
  1586. {
  1587. enum probe_action action = probe_info[i].action;
  1588. for (probe *p : probes[i])
  1589. {
  1590. CORE_ADDR address = p->get_relocated_address (objfile);
  1591. create_solib_event_breakpoint (gdbarch, address);
  1592. register_solib_event_probe (info, objfile, p, address, action);
  1593. }
  1594. }
  1595. svr4_update_solib_event_breakpoints ();
  1596. }
  1597. /* Find all the glibc named probes. Only if all of the probes are found, then
  1598. create them and return true. Otherwise return false. If WITH_PREFIX is set
  1599. then add "rtld" to the front of the probe names. */
  1600. static bool
  1601. svr4_find_and_create_probe_breakpoints (svr4_info *info,
  1602. struct gdbarch *gdbarch,
  1603. struct obj_section *os,
  1604. bool with_prefix)
  1605. {
  1606. std::vector<probe *> probes[NUM_PROBES];
  1607. for (int i = 0; i < NUM_PROBES; i++)
  1608. {
  1609. const char *name = probe_info[i].name;
  1610. char buf[32];
  1611. /* Fedora 17 and Red Hat Enterprise Linux 6.2-6.4 shipped with an early
  1612. version of the probes code in which the probes' names were prefixed
  1613. with "rtld_" and the "map_failed" probe did not exist. The locations
  1614. of the probes are otherwise the same, so we check for probes with
  1615. prefixed names if probes with unprefixed names are not present. */
  1616. if (with_prefix)
  1617. {
  1618. xsnprintf (buf, sizeof (buf), "rtld_%s", name);
  1619. name = buf;
  1620. }
  1621. probes[i] = find_probes_in_objfile (os->objfile, "rtld", name);
  1622. /* The "map_failed" probe did not exist in early
  1623. versions of the probes code in which the probes'
  1624. names were prefixed with "rtld_". */
  1625. if (with_prefix && streq (name, "rtld_map_failed"))
  1626. continue;
  1627. /* Ensure at least one probe for the current name was found. */
  1628. if (probes[i].empty ())
  1629. return false;
  1630. /* Ensure probe arguments can be evaluated. */
  1631. for (probe *p : probes[i])
  1632. {
  1633. if (!p->can_evaluate_arguments ())
  1634. return false;
  1635. /* This will fail if the probe is invalid. This has been seen on Arm
  1636. due to references to symbols that have been resolved away. */
  1637. try
  1638. {
  1639. p->get_argument_count (gdbarch);
  1640. }
  1641. catch (const gdb_exception_error &ex)
  1642. {
  1643. exception_print (gdb_stderr, ex);
  1644. warning (_("Initializing probes-based dynamic linker interface "
  1645. "failed.\nReverting to original interface."));
  1646. return false;
  1647. }
  1648. }
  1649. }
  1650. /* All probes found. Now create them. */
  1651. svr4_create_probe_breakpoints (info, gdbarch, probes, os->objfile);
  1652. return true;
  1653. }
  1654. /* Both the SunOS and the SVR4 dynamic linkers call a marker function
  1655. before and after mapping and unmapping shared libraries. The sole
  1656. purpose of this method is to allow debuggers to set a breakpoint so
  1657. they can track these changes.
  1658. Some versions of the glibc dynamic linker contain named probes
  1659. to allow more fine grained stopping. Given the address of the
  1660. original marker function, this function attempts to find these
  1661. probes, and if found, sets breakpoints on those instead. If the
  1662. probes aren't found, a single breakpoint is set on the original
  1663. marker function. */
  1664. static void
  1665. svr4_create_solib_event_breakpoints (svr4_info *info, struct gdbarch *gdbarch,
  1666. CORE_ADDR address)
  1667. {
  1668. struct obj_section *os = find_pc_section (address);
  1669. if (os == nullptr
  1670. || (!svr4_find_and_create_probe_breakpoints (info, gdbarch, os, false)
  1671. && !svr4_find_and_create_probe_breakpoints (info, gdbarch, os, true)))
  1672. create_solib_event_breakpoint (gdbarch, address);
  1673. }
  1674. /* Helper function for gdb_bfd_lookup_symbol. */
  1675. static int
  1676. cmp_name_and_sec_flags (const asymbol *sym, const void *data)
  1677. {
  1678. return (strcmp (sym->name, (const char *) data) == 0
  1679. && (sym->section->flags & (SEC_CODE | SEC_DATA)) != 0);
  1680. }
  1681. /* Arrange for dynamic linker to hit breakpoint.
  1682. Both the SunOS and the SVR4 dynamic linkers have, as part of their
  1683. debugger interface, support for arranging for the inferior to hit
  1684. a breakpoint after mapping in the shared libraries. This function
  1685. enables that breakpoint.
  1686. For SunOS, there is a special flag location (in_debugger) which we
  1687. set to 1. When the dynamic linker sees this flag set, it will set
  1688. a breakpoint at a location known only to itself, after saving the
  1689. original contents of that place and the breakpoint address itself,
  1690. in it's own internal structures. When we resume the inferior, it
  1691. will eventually take a SIGTRAP when it runs into the breakpoint.
  1692. We handle this (in a different place) by restoring the contents of
  1693. the breakpointed location (which is only known after it stops),
  1694. chasing around to locate the shared libraries that have been
  1695. loaded, then resuming.
  1696. For SVR4, the debugger interface structure contains a member (r_brk)
  1697. which is statically initialized at the time the shared library is
  1698. built, to the offset of a function (_r_debug_state) which is guaran-
  1699. teed to be called once before mapping in a library, and again when
  1700. the mapping is complete. At the time we are examining this member,
  1701. it contains only the unrelocated offset of the function, so we have
  1702. to do our own relocation. Later, when the dynamic linker actually
  1703. runs, it relocates r_brk to be the actual address of _r_debug_state().
  1704. The debugger interface structure also contains an enumeration which
  1705. is set to either RT_ADD or RT_DELETE prior to changing the mapping,
  1706. depending upon whether or not the library is being mapped or unmapped,
  1707. and then set to RT_CONSISTENT after the library is mapped/unmapped. */
  1708. static int
  1709. enable_break (struct svr4_info *info, int from_tty)
  1710. {
  1711. struct bound_minimal_symbol msymbol;
  1712. const char * const *bkpt_namep;
  1713. asection *interp_sect;
  1714. CORE_ADDR sym_addr;
  1715. info->interp_text_sect_low = info->interp_text_sect_high = 0;
  1716. info->interp_plt_sect_low = info->interp_plt_sect_high = 0;
  1717. /* If we already have a shared library list in the target, and
  1718. r_debug contains r_brk, set the breakpoint there - this should
  1719. mean r_brk has already been relocated. Assume the dynamic linker
  1720. is the object containing r_brk. */
  1721. solib_add (NULL, from_tty, auto_solib_add);
  1722. sym_addr = 0;
  1723. if (info->debug_base && solib_svr4_r_map (info) != 0)
  1724. sym_addr = solib_svr4_r_brk (info);
  1725. if (sym_addr != 0)
  1726. {
  1727. struct obj_section *os;
  1728. sym_addr = gdbarch_addr_bits_remove
  1729. (target_gdbarch (),
  1730. gdbarch_convert_from_func_ptr_addr
  1731. (target_gdbarch (), sym_addr, current_inferior ()->top_target ()));
  1732. /* On at least some versions of Solaris there's a dynamic relocation
  1733. on _r_debug.r_brk and SYM_ADDR may not be relocated yet, e.g., if
  1734. we get control before the dynamic linker has self-relocated.
  1735. Check if SYM_ADDR is in a known section, if it is assume we can
  1736. trust its value. This is just a heuristic though, it could go away
  1737. or be replaced if it's getting in the way.
  1738. On ARM we need to know whether the ISA of rtld_db_dlactivity (or
  1739. however it's spelled in your particular system) is ARM or Thumb.
  1740. That knowledge is encoded in the address, if it's Thumb the low bit
  1741. is 1. However, we've stripped that info above and it's not clear
  1742. what all the consequences are of passing a non-addr_bits_remove'd
  1743. address to svr4_create_solib_event_breakpoints. The call to
  1744. find_pc_section verifies we know about the address and have some
  1745. hope of computing the right kind of breakpoint to use (via
  1746. symbol info). It does mean that GDB needs to be pointed at a
  1747. non-stripped version of the dynamic linker in order to obtain
  1748. information it already knows about. Sigh. */
  1749. os = find_pc_section (sym_addr);
  1750. if (os != NULL)
  1751. {
  1752. /* Record the relocated start and end address of the dynamic linker
  1753. text and plt section for svr4_in_dynsym_resolve_code. */
  1754. bfd *tmp_bfd;
  1755. CORE_ADDR load_addr;
  1756. tmp_bfd = os->objfile->obfd;
  1757. load_addr = os->objfile->text_section_offset ();
  1758. interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
  1759. if (interp_sect)
  1760. {
  1761. info->interp_text_sect_low
  1762. = bfd_section_vma (interp_sect) + load_addr;
  1763. info->interp_text_sect_high
  1764. = info->interp_text_sect_low + bfd_section_size (interp_sect);
  1765. }
  1766. interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
  1767. if (interp_sect)
  1768. {
  1769. info->interp_plt_sect_low
  1770. = bfd_section_vma (interp_sect) + load_addr;
  1771. info->interp_plt_sect_high
  1772. = info->interp_plt_sect_low + bfd_section_size (interp_sect);
  1773. }
  1774. svr4_create_solib_event_breakpoints (info, target_gdbarch (), sym_addr);
  1775. return 1;
  1776. }
  1777. }
  1778. /* Find the program interpreter; if not found, warn the user and drop
  1779. into the old breakpoint at symbol code. */
  1780. gdb::optional<gdb::byte_vector> interp_name_holder
  1781. = find_program_interpreter ();
  1782. if (interp_name_holder)
  1783. {
  1784. const char *interp_name = (const char *) interp_name_holder->data ();
  1785. CORE_ADDR load_addr = 0;
  1786. int load_addr_found = 0;
  1787. int loader_found_in_list = 0;
  1788. struct target_ops *tmp_bfd_target;
  1789. sym_addr = 0;
  1790. /* Now we need to figure out where the dynamic linker was
  1791. loaded so that we can load its symbols and place a breakpoint
  1792. in the dynamic linker itself.
  1793. This address is stored on the stack. However, I've been unable
  1794. to find any magic formula to find it for Solaris (appears to
  1795. be trivial on GNU/Linux). Therefore, we have to try an alternate
  1796. mechanism to find the dynamic linker's base address. */
  1797. gdb_bfd_ref_ptr tmp_bfd;
  1798. try
  1799. {
  1800. tmp_bfd = solib_bfd_open (interp_name);
  1801. }
  1802. catch (const gdb_exception &ex)
  1803. {
  1804. }
  1805. if (tmp_bfd == NULL)
  1806. goto bkpt_at_symbol;
  1807. /* Now convert the TMP_BFD into a target. That way target, as
  1808. well as BFD operations can be used. */
  1809. tmp_bfd_target = target_bfd_reopen (tmp_bfd);
  1810. /* On a running target, we can get the dynamic linker's base
  1811. address from the shared library table. */
  1812. for (struct so_list *so : current_program_space->solibs ())
  1813. {
  1814. if (svr4_same_1 (interp_name, so->so_original_name))
  1815. {
  1816. load_addr_found = 1;
  1817. loader_found_in_list = 1;
  1818. load_addr = lm_addr_check (so, tmp_bfd.get ());
  1819. break;
  1820. }
  1821. }
  1822. /* If we were not able to find the base address of the loader
  1823. from our so_list, then try using the AT_BASE auxilliary entry. */
  1824. if (!load_addr_found)
  1825. if (target_auxv_search (current_inferior ()->top_target (),
  1826. AT_BASE, &load_addr) > 0)
  1827. {
  1828. int addr_bit = gdbarch_addr_bit (target_gdbarch ());
  1829. /* Ensure LOAD_ADDR has proper sign in its possible upper bits so
  1830. that `+ load_addr' will overflow CORE_ADDR width not creating
  1831. invalid addresses like 0x101234567 for 32bit inferiors on 64bit
  1832. GDB. */
  1833. if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
  1834. {
  1835. CORE_ADDR space_size = (CORE_ADDR) 1 << addr_bit;
  1836. CORE_ADDR tmp_entry_point = exec_entry_point (tmp_bfd.get (),
  1837. tmp_bfd_target);
  1838. gdb_assert (load_addr < space_size);
  1839. /* TMP_ENTRY_POINT exceeding SPACE_SIZE would be for prelinked
  1840. 64bit ld.so with 32bit executable, it should not happen. */
  1841. if (tmp_entry_point < space_size
  1842. && tmp_entry_point + load_addr >= space_size)
  1843. load_addr -= space_size;
  1844. }
  1845. load_addr_found = 1;
  1846. }
  1847. /* Otherwise we find the dynamic linker's base address by examining
  1848. the current pc (which should point at the entry point for the
  1849. dynamic linker) and subtracting the offset of the entry point.
  1850. This is more fragile than the previous approaches, but is a good
  1851. fallback method because it has actually been working well in
  1852. most cases. */
  1853. if (!load_addr_found)
  1854. {
  1855. struct regcache *regcache
  1856. = get_thread_arch_regcache (current_inferior ()->process_target (),
  1857. inferior_ptid, target_gdbarch ());
  1858. load_addr = (regcache_read_pc (regcache)
  1859. - exec_entry_point (tmp_bfd.get (), tmp_bfd_target));
  1860. }
  1861. if (!loader_found_in_list)
  1862. {
  1863. info->debug_loader_name = xstrdup (interp_name);
  1864. info->debug_loader_offset_p = 1;
  1865. info->debug_loader_offset = load_addr;
  1866. solib_add (NULL, from_tty, auto_solib_add);
  1867. }
  1868. /* Record the relocated start and end address of the dynamic linker
  1869. text and plt section for svr4_in_dynsym_resolve_code. */
  1870. interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".text");
  1871. if (interp_sect)
  1872. {
  1873. info->interp_text_sect_low
  1874. = bfd_section_vma (interp_sect) + load_addr;
  1875. info->interp_text_sect_high
  1876. = info->interp_text_sect_low + bfd_section_size (interp_sect);
  1877. }
  1878. interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".plt");
  1879. if (interp_sect)
  1880. {
  1881. info->interp_plt_sect_low
  1882. = bfd_section_vma (interp_sect) + load_addr;
  1883. info->interp_plt_sect_high
  1884. = info->interp_plt_sect_low + bfd_section_size (interp_sect);
  1885. }
  1886. /* Now try to set a breakpoint in the dynamic linker. */
  1887. for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
  1888. {
  1889. sym_addr = gdb_bfd_lookup_symbol (tmp_bfd.get (),
  1890. cmp_name_and_sec_flags,
  1891. *bkpt_namep);
  1892. if (sym_addr != 0)
  1893. break;
  1894. }
  1895. if (sym_addr != 0)
  1896. /* Convert 'sym_addr' from a function pointer to an address.
  1897. Because we pass tmp_bfd_target instead of the current
  1898. target, this will always produce an unrelocated value. */
  1899. sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
  1900. sym_addr,
  1901. tmp_bfd_target);
  1902. /* We're done with both the temporary bfd and target. Closing
  1903. the target closes the underlying bfd, because it holds the
  1904. only remaining reference. */
  1905. target_close (tmp_bfd_target);
  1906. if (sym_addr != 0)
  1907. {
  1908. svr4_create_solib_event_breakpoints (info, target_gdbarch (),
  1909. load_addr + sym_addr);
  1910. return 1;
  1911. }
  1912. /* For whatever reason we couldn't set a breakpoint in the dynamic
  1913. linker. Warn and drop into the old code. */
  1914. bkpt_at_symbol:
  1915. warning (_("Unable to find dynamic linker breakpoint function.\n"
  1916. "GDB will be unable to debug shared library initializers\n"
  1917. "and track explicitly loaded dynamic code."));
  1918. }
  1919. /* Scan through the lists of symbols, trying to look up the symbol and
  1920. set a breakpoint there. Terminate loop when we/if we succeed. */
  1921. objfile *objf = current_program_space->symfile_object_file;
  1922. for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
  1923. {
  1924. msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, objf);
  1925. if ((msymbol.minsym != NULL)
  1926. && (BMSYMBOL_VALUE_ADDRESS (msymbol) != 0))
  1927. {
  1928. sym_addr = BMSYMBOL_VALUE_ADDRESS (msymbol);
  1929. sym_addr = gdbarch_convert_from_func_ptr_addr
  1930. (target_gdbarch (), sym_addr, current_inferior ()->top_target ());
  1931. svr4_create_solib_event_breakpoints (info, target_gdbarch (),
  1932. sym_addr);
  1933. return 1;
  1934. }
  1935. }
  1936. if (interp_name_holder && !current_inferior ()->attach_flag)
  1937. {
  1938. for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++)
  1939. {
  1940. msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, objf);
  1941. if ((msymbol.minsym != NULL)
  1942. && (BMSYMBOL_VALUE_ADDRESS (msymbol) != 0))
  1943. {
  1944. sym_addr = BMSYMBOL_VALUE_ADDRESS (msymbol);
  1945. sym_addr = gdbarch_convert_from_func_ptr_addr
  1946. (target_gdbarch (), sym_addr,
  1947. current_inferior ()->top_target ());
  1948. svr4_create_solib_event_breakpoints (info, target_gdbarch (),
  1949. sym_addr);
  1950. return 1;
  1951. }
  1952. }
  1953. }
  1954. return 0;
  1955. }
  1956. /* Read the ELF program headers from ABFD. */
  1957. static gdb::optional<gdb::byte_vector>
  1958. read_program_headers_from_bfd (bfd *abfd)
  1959. {
  1960. Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
  1961. int phdrs_size = ehdr->e_phnum * ehdr->e_phentsize;
  1962. if (phdrs_size == 0)
  1963. return {};
  1964. gdb::byte_vector buf (phdrs_size);
  1965. if (bfd_seek (abfd, ehdr->e_phoff, SEEK_SET) != 0
  1966. || bfd_bread (buf.data (), phdrs_size, abfd) != phdrs_size)
  1967. return {};
  1968. return buf;
  1969. }
  1970. /* Return 1 and fill *DISPLACEMENTP with detected PIE offset of inferior
  1971. exec_bfd. Otherwise return 0.
  1972. We relocate all of the sections by the same amount. This
  1973. behavior is mandated by recent editions of the System V ABI.
  1974. According to the System V Application Binary Interface,
  1975. Edition 4.1, page 5-5:
  1976. ... Though the system chooses virtual addresses for
  1977. individual processes, it maintains the segments' relative
  1978. positions. Because position-independent code uses relative
  1979. addressing between segments, the difference between
  1980. virtual addresses in memory must match the difference
  1981. between virtual addresses in the file. The difference
  1982. between the virtual address of any segment in memory and
  1983. the corresponding virtual address in the file is thus a
  1984. single constant value for any one executable or shared
  1985. object in a given process. This difference is the base
  1986. address. One use of the base address is to relocate the
  1987. memory image of the program during dynamic linking.
  1988. The same language also appears in Edition 4.0 of the System V
  1989. ABI and is left unspecified in some of the earlier editions.
  1990. Decide if the objfile needs to be relocated. As indicated above, we will
  1991. only be here when execution is stopped. But during attachment PC can be at
  1992. arbitrary address therefore regcache_read_pc can be misleading (contrary to
  1993. the auxv AT_ENTRY value). Moreover for executable with interpreter section
  1994. regcache_read_pc would point to the interpreter and not the main executable.
  1995. So, to summarize, relocations are necessary when the start address obtained
  1996. from the executable is different from the address in auxv AT_ENTRY entry.
  1997. [ The astute reader will note that we also test to make sure that
  1998. the executable in question has the DYNAMIC flag set. It is my
  1999. opinion that this test is unnecessary (undesirable even). It
  2000. was added to avoid inadvertent relocation of an executable
  2001. whose e_type member in the ELF header is not ET_DYN. There may
  2002. be a time in the future when it is desirable to do relocations
  2003. on other types of files as well in which case this condition
  2004. should either be removed or modified to accomodate the new file
  2005. type. - Kevin, Nov 2000. ] */
  2006. static int
  2007. svr4_exec_displacement (CORE_ADDR *displacementp)
  2008. {
  2009. /* ENTRY_POINT is a possible function descriptor - before
  2010. a call to gdbarch_convert_from_func_ptr_addr. */
  2011. CORE_ADDR entry_point, exec_displacement;
  2012. if (current_program_space->exec_bfd () == NULL)
  2013. return 0;
  2014. /* Therefore for ELF it is ET_EXEC and not ET_DYN. Both shared libraries
  2015. being executed themselves and PIE (Position Independent Executable)
  2016. executables are ET_DYN. */
  2017. if ((bfd_get_file_flags (current_program_space->exec_bfd ()) & DYNAMIC) == 0)
  2018. return 0;
  2019. if (target_auxv_search (current_inferior ()->top_target (),
  2020. AT_ENTRY, &entry_point) <= 0)
  2021. return 0;
  2022. exec_displacement
  2023. = entry_point - bfd_get_start_address (current_program_space->exec_bfd ());
  2024. /* Verify the EXEC_DISPLACEMENT candidate complies with the required page
  2025. alignment. It is cheaper than the program headers comparison below. */
  2026. if (bfd_get_flavour (current_program_space->exec_bfd ())
  2027. == bfd_target_elf_flavour)
  2028. {
  2029. const struct elf_backend_data *elf
  2030. = get_elf_backend_data (current_program_space->exec_bfd ());
  2031. /* p_align of PT_LOAD segments does not specify any alignment but
  2032. only congruency of addresses:
  2033. p_offset % p_align == p_vaddr % p_align
  2034. Kernel is free to load the executable with lower alignment. */
  2035. if ((exec_displacement & (elf->minpagesize - 1)) != 0)
  2036. return 0;
  2037. }
  2038. /* Verify that the auxilliary vector describes the same file as exec_bfd, by
  2039. comparing their program headers. If the program headers in the auxilliary
  2040. vector do not match the program headers in the executable, then we are
  2041. looking at a different file than the one used by the kernel - for
  2042. instance, "gdb program" connected to "gdbserver :PORT ld.so program". */
  2043. if (bfd_get_flavour (current_program_space->exec_bfd ())
  2044. == bfd_target_elf_flavour)
  2045. {
  2046. /* Be optimistic and return 0 only if GDB was able to verify the headers
  2047. really do not match. */
  2048. int arch_size;
  2049. gdb::optional<gdb::byte_vector> phdrs_target
  2050. = read_program_header (-1, &arch_size, NULL);
  2051. gdb::optional<gdb::byte_vector> phdrs_binary
  2052. = read_program_headers_from_bfd (current_program_space->exec_bfd ());
  2053. if (phdrs_target && phdrs_binary)
  2054. {
  2055. enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  2056. /* We are dealing with three different addresses. EXEC_BFD
  2057. represents current address in on-disk file. target memory content
  2058. may be different from EXEC_BFD as the file may have been prelinked
  2059. to a different address after the executable has been loaded.
  2060. Moreover the address of placement in target memory can be
  2061. different from what the program headers in target memory say -
  2062. this is the goal of PIE.
  2063. Detected DISPLACEMENT covers both the offsets of PIE placement and
  2064. possible new prelink performed after start of the program. Here
  2065. relocate BUF and BUF2 just by the EXEC_BFD vs. target memory
  2066. content offset for the verification purpose. */
  2067. if (phdrs_target->size () != phdrs_binary->size ()
  2068. || bfd_get_arch_size (current_program_space->exec_bfd ()) != arch_size)
  2069. return 0;
  2070. else if (arch_size == 32
  2071. && phdrs_target->size () >= sizeof (Elf32_External_Phdr)
  2072. && phdrs_target->size () % sizeof (Elf32_External_Phdr) == 0)
  2073. {
  2074. Elf_Internal_Ehdr *ehdr2
  2075. = elf_tdata (current_program_space->exec_bfd ())->elf_header;
  2076. Elf_Internal_Phdr *phdr2
  2077. = elf_tdata (current_program_space->exec_bfd ())->phdr;
  2078. CORE_ADDR displacement = 0;
  2079. int i;
  2080. /* DISPLACEMENT could be found more easily by the difference of
  2081. ehdr2->e_entry. But we haven't read the ehdr yet, and we
  2082. already have enough information to compute that displacement
  2083. with what we've read. */
  2084. for (i = 0; i < ehdr2->e_phnum; i++)
  2085. if (phdr2[i].p_type == PT_LOAD)
  2086. {
  2087. Elf32_External_Phdr *phdrp;
  2088. gdb_byte *buf_vaddr_p, *buf_paddr_p;
  2089. CORE_ADDR vaddr, paddr;
  2090. CORE_ADDR displacement_vaddr = 0;
  2091. CORE_ADDR displacement_paddr = 0;
  2092. phdrp = &((Elf32_External_Phdr *) phdrs_target->data ())[i];
  2093. buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
  2094. buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
  2095. vaddr = extract_unsigned_integer (buf_vaddr_p, 4,
  2096. byte_order);
  2097. displacement_vaddr = vaddr - phdr2[i].p_vaddr;
  2098. paddr = extract_unsigned_integer (buf_paddr_p, 4,
  2099. byte_order);
  2100. displacement_paddr = paddr - phdr2[i].p_paddr;
  2101. if (displacement_vaddr == displacement_paddr)
  2102. displacement = displacement_vaddr;
  2103. break;
  2104. }
  2105. /* Now compare program headers from the target and the binary
  2106. with optional DISPLACEMENT. */
  2107. for (i = 0;
  2108. i < phdrs_target->size () / sizeof (Elf32_External_Phdr);
  2109. i++)
  2110. {
  2111. Elf32_External_Phdr *phdrp;
  2112. Elf32_External_Phdr *phdr2p;
  2113. gdb_byte *buf_vaddr_p, *buf_paddr_p;
  2114. CORE_ADDR vaddr, paddr;
  2115. asection *plt2_asect;
  2116. phdrp = &((Elf32_External_Phdr *) phdrs_target->data ())[i];
  2117. buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
  2118. buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
  2119. phdr2p = &((Elf32_External_Phdr *) phdrs_binary->data ())[i];
  2120. /* PT_GNU_STACK is an exception by being never relocated by
  2121. prelink as its addresses are always zero. */
  2122. if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
  2123. continue;
  2124. /* Check also other adjustment combinations - PR 11786. */
  2125. vaddr = extract_unsigned_integer (buf_vaddr_p, 4,
  2126. byte_order);
  2127. vaddr -= displacement;
  2128. store_unsigned_integer (buf_vaddr_p, 4, byte_order, vaddr);
  2129. paddr = extract_unsigned_integer (buf_paddr_p, 4,
  2130. byte_order);
  2131. paddr -= displacement;
  2132. store_unsigned_integer (buf_paddr_p, 4, byte_order, paddr);
  2133. if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
  2134. continue;
  2135. /* Strip modifies the flags and alignment of PT_GNU_RELRO.
  2136. CentOS-5 has problems with filesz, memsz as well.
  2137. Strip also modifies memsz of PT_TLS.
  2138. See PR 11786. */
  2139. if (phdr2[i].p_type == PT_GNU_RELRO
  2140. || phdr2[i].p_type == PT_TLS)
  2141. {
  2142. Elf32_External_Phdr tmp_phdr = *phdrp;
  2143. Elf32_External_Phdr tmp_phdr2 = *phdr2p;
  2144. memset (tmp_phdr.p_filesz, 0, 4);
  2145. memset (tmp_phdr.p_memsz, 0, 4);
  2146. memset (tmp_phdr.p_flags, 0, 4);
  2147. memset (tmp_phdr.p_align, 0, 4);
  2148. memset (tmp_phdr2.p_filesz, 0, 4);
  2149. memset (tmp_phdr2.p_memsz, 0, 4);
  2150. memset (tmp_phdr2.p_flags, 0, 4);
  2151. memset (tmp_phdr2.p_align, 0, 4);
  2152. if (memcmp (&tmp_phdr, &tmp_phdr2, sizeof (tmp_phdr))
  2153. == 0)
  2154. continue;
  2155. }
  2156. /* prelink can convert .plt SHT_NOBITS to SHT_PROGBITS. */
  2157. bfd *exec_bfd = current_program_space->exec_bfd ();
  2158. plt2_asect = bfd_get_section_by_name (exec_bfd, ".plt");
  2159. if (plt2_asect)
  2160. {
  2161. int content2;
  2162. gdb_byte *buf_filesz_p = (gdb_byte *) &phdrp->p_filesz;
  2163. CORE_ADDR filesz;
  2164. content2 = (bfd_section_flags (plt2_asect)
  2165. & SEC_HAS_CONTENTS) != 0;
  2166. filesz = extract_unsigned_integer (buf_filesz_p, 4,
  2167. byte_order);
  2168. /* PLT2_ASECT is from on-disk file (exec_bfd) while
  2169. FILESZ is from the in-memory image. */
  2170. if (content2)
  2171. filesz += bfd_section_size (plt2_asect);
  2172. else
  2173. filesz -= bfd_section_size (plt2_asect);
  2174. store_unsigned_integer (buf_filesz_p, 4, byte_order,
  2175. filesz);
  2176. if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
  2177. continue;
  2178. }
  2179. return 0;
  2180. }
  2181. }
  2182. else if (arch_size == 64
  2183. && phdrs_target->size () >= sizeof (Elf64_External_Phdr)
  2184. && phdrs_target->size () % sizeof (Elf64_External_Phdr) == 0)
  2185. {
  2186. Elf_Internal_Ehdr *ehdr2
  2187. = elf_tdata (current_program_space->exec_bfd ())->elf_header;
  2188. Elf_Internal_Phdr *phdr2
  2189. = elf_tdata (current_program_space->exec_bfd ())->phdr;
  2190. CORE_ADDR displacement = 0;
  2191. int i;
  2192. /* DISPLACEMENT could be found more easily by the difference of
  2193. ehdr2->e_entry. But we haven't read the ehdr yet, and we
  2194. already have enough information to compute that displacement
  2195. with what we've read. */
  2196. for (i = 0; i < ehdr2->e_phnum; i++)
  2197. if (phdr2[i].p_type == PT_LOAD)
  2198. {
  2199. Elf64_External_Phdr *phdrp;
  2200. gdb_byte *buf_vaddr_p, *buf_paddr_p;
  2201. CORE_ADDR vaddr, paddr;
  2202. CORE_ADDR displacement_vaddr = 0;
  2203. CORE_ADDR displacement_paddr = 0;
  2204. phdrp = &((Elf64_External_Phdr *) phdrs_target->data ())[i];
  2205. buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
  2206. buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
  2207. vaddr = extract_unsigned_integer (buf_vaddr_p, 8,
  2208. byte_order);
  2209. displacement_vaddr = vaddr - phdr2[i].p_vaddr;
  2210. paddr = extract_unsigned_integer (buf_paddr_p, 8,
  2211. byte_order);
  2212. displacement_paddr = paddr - phdr2[i].p_paddr;
  2213. if (displacement_vaddr == displacement_paddr)
  2214. displacement = displacement_vaddr;
  2215. break;
  2216. }
  2217. /* Now compare BUF and BUF2 with optional DISPLACEMENT. */
  2218. for (i = 0;
  2219. i < phdrs_target->size () / sizeof (Elf64_External_Phdr);
  2220. i++)
  2221. {
  2222. Elf64_External_Phdr *phdrp;
  2223. Elf64_External_Phdr *phdr2p;
  2224. gdb_byte *buf_vaddr_p, *buf_paddr_p;
  2225. CORE_ADDR vaddr, paddr;
  2226. asection *plt2_asect;
  2227. phdrp = &((Elf64_External_Phdr *) phdrs_target->data ())[i];
  2228. buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
  2229. buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
  2230. phdr2p = &((Elf64_External_Phdr *) phdrs_binary->data ())[i];
  2231. /* PT_GNU_STACK is an exception by being never relocated by
  2232. prelink as its addresses are always zero. */
  2233. if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
  2234. continue;
  2235. /* Check also other adjustment combinations - PR 11786. */
  2236. vaddr = extract_unsigned_integer (buf_vaddr_p, 8,
  2237. byte_order);
  2238. vaddr -= displacement;
  2239. store_unsigned_integer (buf_vaddr_p, 8, byte_order, vaddr);
  2240. paddr = extract_unsigned_integer (buf_paddr_p, 8,
  2241. byte_order);
  2242. paddr -= displacement;
  2243. store_unsigned_integer (buf_paddr_p, 8, byte_order, paddr);
  2244. if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
  2245. continue;
  2246. /* Strip modifies the flags and alignment of PT_GNU_RELRO.
  2247. CentOS-5 has problems with filesz, memsz as well.
  2248. Strip also modifies memsz of PT_TLS.
  2249. See PR 11786. */
  2250. if (phdr2[i].p_type == PT_GNU_RELRO
  2251. || phdr2[i].p_type == PT_TLS)
  2252. {
  2253. Elf64_External_Phdr tmp_phdr = *phdrp;
  2254. Elf64_External_Phdr tmp_phdr2 = *phdr2p;
  2255. memset (tmp_phdr.p_filesz, 0, 8);
  2256. memset (tmp_phdr.p_memsz, 0, 8);
  2257. memset (tmp_phdr.p_flags, 0, 4);
  2258. memset (tmp_phdr.p_align, 0, 8);
  2259. memset (tmp_phdr2.p_filesz, 0, 8);
  2260. memset (tmp_phdr2.p_memsz, 0, 8);
  2261. memset (tmp_phdr2.p_flags, 0, 4);
  2262. memset (tmp_phdr2.p_align, 0, 8);
  2263. if (memcmp (&tmp_phdr, &tmp_phdr2, sizeof (tmp_phdr))
  2264. == 0)
  2265. continue;
  2266. }
  2267. /* prelink can convert .plt SHT_NOBITS to SHT_PROGBITS. */
  2268. plt2_asect
  2269. = bfd_get_section_by_name (current_program_space->exec_bfd (),
  2270. ".plt");
  2271. if (plt2_asect)
  2272. {
  2273. int content2;
  2274. gdb_byte *buf_filesz_p = (gdb_byte *) &phdrp->p_filesz;
  2275. CORE_ADDR filesz;
  2276. content2 = (bfd_section_flags (plt2_asect)
  2277. & SEC_HAS_CONTENTS) != 0;
  2278. filesz = extract_unsigned_integer (buf_filesz_p, 8,
  2279. byte_order);
  2280. /* PLT2_ASECT is from on-disk file (current
  2281. exec_bfd) while FILESZ is from the in-memory
  2282. image. */
  2283. if (content2)
  2284. filesz += bfd_section_size (plt2_asect);
  2285. else
  2286. filesz -= bfd_section_size (plt2_asect);
  2287. store_unsigned_integer (buf_filesz_p, 8, byte_order,
  2288. filesz);
  2289. if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
  2290. continue;
  2291. }
  2292. return 0;
  2293. }
  2294. }
  2295. else
  2296. return 0;
  2297. }
  2298. }
  2299. if (info_verbose)
  2300. {
  2301. /* It can be printed repeatedly as there is no easy way to check
  2302. the executable symbols/file has been already relocated to
  2303. displacement. */
  2304. gdb_printf (_("Using PIE (Position Independent Executable) "
  2305. "displacement %s for \"%s\".\n"),
  2306. paddress (target_gdbarch (), exec_displacement),
  2307. bfd_get_filename (current_program_space->exec_bfd ()));
  2308. }
  2309. *displacementp = exec_displacement;
  2310. return 1;
  2311. }
  2312. /* Relocate the main executable. This function should be called upon
  2313. stopping the inferior process at the entry point to the program.
  2314. The entry point from BFD is compared to the AT_ENTRY of AUXV and if they are
  2315. different, the main executable is relocated by the proper amount. */
  2316. static void
  2317. svr4_relocate_main_executable (void)
  2318. {
  2319. CORE_ADDR displacement;
  2320. /* If we are re-running this executable, SYMFILE_OBJFILE->SECTION_OFFSETS
  2321. probably contains the offsets computed using the PIE displacement
  2322. from the previous run, which of course are irrelevant for this run.
  2323. So we need to determine the new PIE displacement and recompute the
  2324. section offsets accordingly, even if SYMFILE_OBJFILE->SECTION_OFFSETS
  2325. already contains pre-computed offsets.
  2326. If we cannot compute the PIE displacement, either:
  2327. - The executable is not PIE.
  2328. - SYMFILE_OBJFILE does not match the executable started in the target.
  2329. This can happen for main executable symbols loaded at the host while
  2330. `ld.so --ld-args main-executable' is loaded in the target.
  2331. Then we leave the section offsets untouched and use them as is for
  2332. this run. Either:
  2333. - These section offsets were properly reset earlier, and thus
  2334. already contain the correct values. This can happen for instance
  2335. when reconnecting via the remote protocol to a target that supports
  2336. the `qOffsets' packet.
  2337. - The section offsets were not reset earlier, and the best we can
  2338. hope is that the old offsets are still applicable to the new run. */
  2339. if (! svr4_exec_displacement (&displacement))
  2340. return;
  2341. /* Even DISPLACEMENT 0 is a valid new difference of in-memory vs. in-file
  2342. addresses. */
  2343. objfile *objf = current_program_space->symfile_object_file;
  2344. if (objf)
  2345. {
  2346. section_offsets new_offsets (objf->section_offsets.size (),
  2347. displacement);
  2348. objfile_relocate (objf, new_offsets);
  2349. }
  2350. else if (current_program_space->exec_bfd ())
  2351. {
  2352. asection *asect;
  2353. bfd *exec_bfd = current_program_space->exec_bfd ();
  2354. for (asect = exec_bfd->sections; asect != NULL; asect = asect->next)
  2355. exec_set_section_address (bfd_get_filename (exec_bfd), asect->index,
  2356. bfd_section_vma (asect) + displacement);
  2357. }
  2358. }
  2359. /* Implement the "create_inferior_hook" target_solib_ops method.
  2360. For SVR4 executables, this first instruction is either the first
  2361. instruction in the dynamic linker (for dynamically linked
  2362. executables) or the instruction at "start" for statically linked
  2363. executables. For dynamically linked executables, the system
  2364. first exec's /lib/libc.so.N, which contains the dynamic linker,
  2365. and starts it running. The dynamic linker maps in any needed
  2366. shared libraries, maps in the actual user executable, and then
  2367. jumps to "start" in the user executable.
  2368. We can arrange to cooperate with the dynamic linker to discover the
  2369. names of shared libraries that are dynamically linked, and the base
  2370. addresses to which they are linked.
  2371. This function is responsible for discovering those names and
  2372. addresses, and saving sufficient information about them to allow
  2373. their symbols to be read at a later time. */
  2374. static void
  2375. svr4_solib_create_inferior_hook (int from_tty)
  2376. {
  2377. struct svr4_info *info;
  2378. info = get_svr4_info (current_program_space);
  2379. /* Clear the probes-based interface's state. */
  2380. free_probes_table (info);
  2381. free_solib_list (info);
  2382. /* Relocate the main executable if necessary. */
  2383. svr4_relocate_main_executable ();
  2384. /* No point setting a breakpoint in the dynamic linker if we can't
  2385. hit it (e.g., a core file, or a trace file). */
  2386. if (!target_has_execution ())
  2387. return;
  2388. if (!svr4_have_link_map_offsets ())
  2389. return;
  2390. if (!enable_break (info, from_tty))
  2391. return;
  2392. }
  2393. static void
  2394. svr4_clear_solib (void)
  2395. {
  2396. struct svr4_info *info;
  2397. info = get_svr4_info (current_program_space);
  2398. info->debug_base = 0;
  2399. info->debug_loader_offset_p = 0;
  2400. info->debug_loader_offset = 0;
  2401. xfree (info->debug_loader_name);
  2402. info->debug_loader_name = NULL;
  2403. }
  2404. /* Clear any bits of ADDR that wouldn't fit in a target-format
  2405. data pointer. "Data pointer" here refers to whatever sort of
  2406. address the dynamic linker uses to manage its sections. At the
  2407. moment, we don't support shared libraries on any processors where
  2408. code and data pointers are different sizes.
  2409. This isn't really the right solution. What we really need here is
  2410. a way to do arithmetic on CORE_ADDR values that respects the
  2411. natural pointer/address correspondence. (For example, on the MIPS,
  2412. converting a 32-bit pointer to a 64-bit CORE_ADDR requires you to
  2413. sign-extend the value. There, simply truncating the bits above
  2414. gdbarch_ptr_bit, as we do below, is no good.) This should probably
  2415. be a new gdbarch method or something. */
  2416. static CORE_ADDR
  2417. svr4_truncate_ptr (CORE_ADDR addr)
  2418. {
  2419. if (gdbarch_ptr_bit (target_gdbarch ()) == sizeof (CORE_ADDR) * 8)
  2420. /* We don't need to truncate anything, and the bit twiddling below
  2421. will fail due to overflow problems. */
  2422. return addr;
  2423. else
  2424. return addr & (((CORE_ADDR) 1 << gdbarch_ptr_bit (target_gdbarch ())) - 1);
  2425. }
  2426. static void
  2427. svr4_relocate_section_addresses (struct so_list *so,
  2428. struct target_section *sec)
  2429. {
  2430. bfd *abfd = sec->the_bfd_section->owner;
  2431. sec->addr = svr4_truncate_ptr (sec->addr + lm_addr_check (so, abfd));
  2432. sec->endaddr = svr4_truncate_ptr (sec->endaddr + lm_addr_check (so, abfd));
  2433. }
  2434. /* Architecture-specific operations. */
  2435. /* Per-architecture data key. */
  2436. static struct gdbarch_data *solib_svr4_data;
  2437. struct solib_svr4_ops
  2438. {
  2439. /* Return a description of the layout of `struct link_map'. */
  2440. struct link_map_offsets *(*fetch_link_map_offsets)(void);
  2441. };
  2442. /* Return a default for the architecture-specific operations. */
  2443. static void *
  2444. solib_svr4_init (struct obstack *obstack)
  2445. {
  2446. struct solib_svr4_ops *ops;
  2447. ops = OBSTACK_ZALLOC (obstack, struct solib_svr4_ops);
  2448. ops->fetch_link_map_offsets = NULL;
  2449. return ops;
  2450. }
  2451. /* Set the architecture-specific `struct link_map_offsets' fetcher for
  2452. GDBARCH to FLMO. Also, install SVR4 solib_ops into GDBARCH. */
  2453. void
  2454. set_solib_svr4_fetch_link_map_offsets (struct gdbarch *gdbarch,
  2455. struct link_map_offsets *(*flmo) (void))
  2456. {
  2457. struct solib_svr4_ops *ops
  2458. = (struct solib_svr4_ops *) gdbarch_data (gdbarch, solib_svr4_data);
  2459. ops->fetch_link_map_offsets = flmo;
  2460. set_solib_ops (gdbarch, &svr4_so_ops);
  2461. set_gdbarch_iterate_over_objfiles_in_search_order
  2462. (gdbarch, svr4_iterate_over_objfiles_in_search_order);
  2463. }
  2464. /* Fetch a link_map_offsets structure using the architecture-specific
  2465. `struct link_map_offsets' fetcher. */
  2466. static struct link_map_offsets *
  2467. svr4_fetch_link_map_offsets (void)
  2468. {
  2469. struct solib_svr4_ops *ops
  2470. = (struct solib_svr4_ops *) gdbarch_data (target_gdbarch (),
  2471. solib_svr4_data);
  2472. gdb_assert (ops->fetch_link_map_offsets);
  2473. return ops->fetch_link_map_offsets ();
  2474. }
  2475. /* Return 1 if a link map offset fetcher has been defined, 0 otherwise. */
  2476. static int
  2477. svr4_have_link_map_offsets (void)
  2478. {
  2479. struct solib_svr4_ops *ops
  2480. = (struct solib_svr4_ops *) gdbarch_data (target_gdbarch (),
  2481. solib_svr4_data);
  2482. return (ops->fetch_link_map_offsets != NULL);
  2483. }
  2484. /* Most OS'es that have SVR4-style ELF dynamic libraries define a
  2485. `struct r_debug' and a `struct link_map' that are binary compatible
  2486. with the original SVR4 implementation. */
  2487. /* Fetch (and possibly build) an appropriate `struct link_map_offsets'
  2488. for an ILP32 SVR4 system. */
  2489. struct link_map_offsets *
  2490. svr4_ilp32_fetch_link_map_offsets (void)
  2491. {
  2492. static struct link_map_offsets lmo;
  2493. static struct link_map_offsets *lmp = NULL;
  2494. if (lmp == NULL)
  2495. {
  2496. lmp = &lmo;
  2497. lmo.r_version_offset = 0;
  2498. lmo.r_version_size = 4;
  2499. lmo.r_map_offset = 4;
  2500. lmo.r_brk_offset = 8;
  2501. lmo.r_ldsomap_offset = 20;
  2502. /* Everything we need is in the first 20 bytes. */
  2503. lmo.link_map_size = 20;
  2504. lmo.l_addr_offset = 0;
  2505. lmo.l_name_offset = 4;
  2506. lmo.l_ld_offset = 8;
  2507. lmo.l_next_offset = 12;
  2508. lmo.l_prev_offset = 16;
  2509. }
  2510. return lmp;
  2511. }
  2512. /* Fetch (and possibly build) an appropriate `struct link_map_offsets'
  2513. for an LP64 SVR4 system. */
  2514. struct link_map_offsets *
  2515. svr4_lp64_fetch_link_map_offsets (void)
  2516. {
  2517. static struct link_map_offsets lmo;
  2518. static struct link_map_offsets *lmp = NULL;
  2519. if (lmp == NULL)
  2520. {
  2521. lmp = &lmo;
  2522. lmo.r_version_offset = 0;
  2523. lmo.r_version_size = 4;
  2524. lmo.r_map_offset = 8;
  2525. lmo.r_brk_offset = 16;
  2526. lmo.r_ldsomap_offset = 40;
  2527. /* Everything we need is in the first 40 bytes. */
  2528. lmo.link_map_size = 40;
  2529. lmo.l_addr_offset = 0;
  2530. lmo.l_name_offset = 8;
  2531. lmo.l_ld_offset = 16;
  2532. lmo.l_next_offset = 24;
  2533. lmo.l_prev_offset = 32;
  2534. }
  2535. return lmp;
  2536. }
  2537. struct target_so_ops svr4_so_ops;
  2538. /* Search order for ELF DSOs linked with -Bsymbolic. Those DSOs have a
  2539. different rule for symbol lookup. The lookup begins here in the DSO, not in
  2540. the main executable. */
  2541. static void
  2542. svr4_iterate_over_objfiles_in_search_order
  2543. (struct gdbarch *gdbarch,
  2544. iterate_over_objfiles_in_search_order_cb_ftype *cb,
  2545. void *cb_data, struct objfile *current_objfile)
  2546. {
  2547. bool checked_current_objfile = false;
  2548. if (current_objfile != nullptr)
  2549. {
  2550. bfd *abfd;
  2551. if (current_objfile->separate_debug_objfile_backlink != nullptr)
  2552. current_objfile = current_objfile->separate_debug_objfile_backlink;
  2553. if (current_objfile == current_program_space->symfile_object_file)
  2554. abfd = current_program_space->exec_bfd ();
  2555. else
  2556. abfd = current_objfile->obfd;
  2557. if (abfd != nullptr
  2558. && gdb_bfd_scan_elf_dyntag (DT_SYMBOLIC, abfd, nullptr, nullptr) == 1)
  2559. {
  2560. checked_current_objfile = true;
  2561. if (cb (current_objfile, cb_data) != 0)
  2562. return;
  2563. }
  2564. }
  2565. for (objfile *objfile : current_program_space->objfiles ())
  2566. {
  2567. if (checked_current_objfile && objfile == current_objfile)
  2568. continue;
  2569. if (cb (objfile, cb_data) != 0)
  2570. return;
  2571. }
  2572. }
  2573. void _initialize_svr4_solib ();
  2574. void
  2575. _initialize_svr4_solib ()
  2576. {
  2577. solib_svr4_data = gdbarch_data_register_pre_init (solib_svr4_init);
  2578. svr4_so_ops.relocate_section_addresses = svr4_relocate_section_addresses;
  2579. svr4_so_ops.free_so = svr4_free_so;
  2580. svr4_so_ops.clear_so = svr4_clear_so;
  2581. svr4_so_ops.clear_solib = svr4_clear_solib;
  2582. svr4_so_ops.solib_create_inferior_hook = svr4_solib_create_inferior_hook;
  2583. svr4_so_ops.current_sos = svr4_current_sos;
  2584. svr4_so_ops.open_symbol_file_object = open_symbol_file_object;
  2585. svr4_so_ops.in_dynsym_resolve_code = svr4_in_dynsym_resolve_code;
  2586. svr4_so_ops.bfd_open = solib_bfd_open;
  2587. svr4_so_ops.same = svr4_same;
  2588. svr4_so_ops.keep_data_in_core = svr4_keep_data_in_core;
  2589. svr4_so_ops.update_breakpoints = svr4_update_solib_event_breakpoints;
  2590. svr4_so_ops.handle_event = svr4_handle_solib_event;
  2591. gdb::observers::free_objfile.attach (svr4_free_objfile_observer,
  2592. "solib-svr4");
  2593. }