symfile.c 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952
  1. /* Generic symbol file reading for the GNU debugger, GDB.
  2. Copyright (C) 1990-2022 Free Software Foundation, Inc.
  3. Contributed by Cygnus Support, using pieces from other GDB modules.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "arch-utils.h"
  17. #include "bfdlink.h"
  18. #include "symtab.h"
  19. #include "gdbtypes.h"
  20. #include "gdbcore.h"
  21. #include "frame.h"
  22. #include "target.h"
  23. #include "value.h"
  24. #include "symfile.h"
  25. #include "objfiles.h"
  26. #include "source.h"
  27. #include "gdbcmd.h"
  28. #include "breakpoint.h"
  29. #include "language.h"
  30. #include "complaints.h"
  31. #include "demangle.h"
  32. #include "inferior.h"
  33. #include "regcache.h"
  34. #include "filenames.h" /* for DOSish file names */
  35. #include "gdb-stabs.h"
  36. #include "gdbsupport/gdb_obstack.h"
  37. #include "completer.h"
  38. #include "bcache.h"
  39. #include "hashtab.h"
  40. #include "readline/tilde.h"
  41. #include "block.h"
  42. #include "observable.h"
  43. #include "exec.h"
  44. #include "parser-defs.h"
  45. #include "varobj.h"
  46. #include "elf-bfd.h"
  47. #include "solib.h"
  48. #include "remote.h"
  49. #include "stack.h"
  50. #include "gdb_bfd.h"
  51. #include "cli/cli-utils.h"
  52. #include "gdbsupport/byte-vector.h"
  53. #include "gdbsupport/pathstuff.h"
  54. #include "gdbsupport/selftest.h"
  55. #include "cli/cli-style.h"
  56. #include "gdbsupport/forward-scope-exit.h"
  57. #include "gdbsupport/buildargv.h"
  58. #include <sys/types.h>
  59. #include <fcntl.h>
  60. #include <sys/stat.h>
  61. #include <ctype.h>
  62. #include <chrono>
  63. #include <algorithm>
  64. int (*deprecated_ui_load_progress_hook) (const char *section,
  65. unsigned long num);
  66. void (*deprecated_show_load_progress) (const char *section,
  67. unsigned long section_sent,
  68. unsigned long section_size,
  69. unsigned long total_sent,
  70. unsigned long total_size);
  71. void (*deprecated_pre_add_symbol_hook) (const char *);
  72. void (*deprecated_post_add_symbol_hook) (void);
  73. using clear_symtab_users_cleanup
  74. = FORWARD_SCOPE_EXIT (clear_symtab_users);
  75. /* Global variables owned by this file. */
  76. /* See symfile.h. */
  77. int readnow_symbol_files;
  78. /* See symfile.h. */
  79. int readnever_symbol_files;
  80. /* Functions this file defines. */
  81. static void symbol_file_add_main_1 (const char *args, symfile_add_flags add_flags,
  82. objfile_flags flags, CORE_ADDR reloff);
  83. static const struct sym_fns *find_sym_fns (bfd *);
  84. static void overlay_invalidate_all (void);
  85. static void simple_free_overlay_table (void);
  86. static void read_target_long_array (CORE_ADDR, unsigned int *, int, int,
  87. enum bfd_endian);
  88. static int simple_read_overlay_table (void);
  89. static int simple_overlay_update_1 (struct obj_section *);
  90. static void symfile_find_segment_sections (struct objfile *objfile);
  91. /* List of all available sym_fns. On gdb startup, each object file reader
  92. calls add_symtab_fns() to register information on each format it is
  93. prepared to read. */
  94. struct registered_sym_fns
  95. {
  96. registered_sym_fns (bfd_flavour sym_flavour_, const struct sym_fns *sym_fns_)
  97. : sym_flavour (sym_flavour_), sym_fns (sym_fns_)
  98. {}
  99. /* BFD flavour that we handle. */
  100. enum bfd_flavour sym_flavour;
  101. /* The "vtable" of symbol functions. */
  102. const struct sym_fns *sym_fns;
  103. };
  104. static std::vector<registered_sym_fns> symtab_fns;
  105. /* Values for "set print symbol-loading". */
  106. const char print_symbol_loading_off[] = "off";
  107. const char print_symbol_loading_brief[] = "brief";
  108. const char print_symbol_loading_full[] = "full";
  109. static const char *print_symbol_loading_enums[] =
  110. {
  111. print_symbol_loading_off,
  112. print_symbol_loading_brief,
  113. print_symbol_loading_full,
  114. NULL
  115. };
  116. static const char *print_symbol_loading = print_symbol_loading_full;
  117. /* See symfile.h. */
  118. bool auto_solib_add = true;
  119. /* Return non-zero if symbol-loading messages should be printed.
  120. FROM_TTY is the standard from_tty argument to gdb commands.
  121. If EXEC is non-zero the messages are for the executable.
  122. Otherwise, messages are for shared libraries.
  123. If FULL is non-zero then the caller is printing a detailed message.
  124. E.g., the message includes the shared library name.
  125. Otherwise, the caller is printing a brief "summary" message. */
  126. int
  127. print_symbol_loading_p (int from_tty, int exec, int full)
  128. {
  129. if (!from_tty && !info_verbose)
  130. return 0;
  131. if (exec)
  132. {
  133. /* We don't check FULL for executables, there are few such
  134. messages, therefore brief == full. */
  135. return print_symbol_loading != print_symbol_loading_off;
  136. }
  137. if (full)
  138. return print_symbol_loading == print_symbol_loading_full;
  139. return print_symbol_loading == print_symbol_loading_brief;
  140. }
  141. /* True if we are reading a symbol table. */
  142. int currently_reading_symtab = 0;
  143. /* Increment currently_reading_symtab and return a cleanup that can be
  144. used to decrement it. */
  145. scoped_restore_tmpl<int>
  146. increment_reading_symtab (void)
  147. {
  148. gdb_assert (currently_reading_symtab >= 0);
  149. return make_scoped_restore (&currently_reading_symtab,
  150. currently_reading_symtab + 1);
  151. }
  152. /* Remember the lowest-addressed loadable section we've seen.
  153. In case of equal vmas, the section with the largest size becomes the
  154. lowest-addressed loadable section.
  155. If the vmas and sizes are equal, the last section is considered the
  156. lowest-addressed loadable section. */
  157. static void
  158. find_lowest_section (asection *sect, asection **lowest)
  159. {
  160. if (0 == (bfd_section_flags (sect) & (SEC_ALLOC | SEC_LOAD)))
  161. return;
  162. if (!*lowest)
  163. *lowest = sect; /* First loadable section */
  164. else if (bfd_section_vma (*lowest) > bfd_section_vma (sect))
  165. *lowest = sect; /* A lower loadable section */
  166. else if (bfd_section_vma (*lowest) == bfd_section_vma (sect)
  167. && (bfd_section_size (*lowest) <= bfd_section_size (sect)))
  168. *lowest = sect;
  169. }
  170. /* Build (allocate and populate) a section_addr_info struct from
  171. an existing section table. */
  172. section_addr_info
  173. build_section_addr_info_from_section_table (const target_section_table &table)
  174. {
  175. section_addr_info sap;
  176. for (const target_section &stp : table)
  177. {
  178. struct bfd_section *asect = stp.the_bfd_section;
  179. bfd *abfd = asect->owner;
  180. if (bfd_section_flags (asect) & (SEC_ALLOC | SEC_LOAD)
  181. && sap.size () < table.size ())
  182. sap.emplace_back (stp.addr,
  183. bfd_section_name (asect),
  184. gdb_bfd_section_index (abfd, asect));
  185. }
  186. return sap;
  187. }
  188. /* Create a section_addr_info from section offsets in ABFD. */
  189. static section_addr_info
  190. build_section_addr_info_from_bfd (bfd *abfd)
  191. {
  192. struct bfd_section *sec;
  193. section_addr_info sap;
  194. for (sec = abfd->sections; sec != NULL; sec = sec->next)
  195. if (bfd_section_flags (sec) & (SEC_ALLOC | SEC_LOAD))
  196. sap.emplace_back (bfd_section_vma (sec),
  197. bfd_section_name (sec),
  198. gdb_bfd_section_index (abfd, sec));
  199. return sap;
  200. }
  201. /* Create a section_addr_info from section offsets in OBJFILE. */
  202. section_addr_info
  203. build_section_addr_info_from_objfile (const struct objfile *objfile)
  204. {
  205. int i;
  206. /* Before reread_symbols gets rewritten it is not safe to call:
  207. gdb_assert (objfile->num_sections == bfd_count_sections (objfile->obfd));
  208. */
  209. section_addr_info sap = build_section_addr_info_from_bfd (objfile->obfd);
  210. for (i = 0; i < sap.size (); i++)
  211. {
  212. int sectindex = sap[i].sectindex;
  213. sap[i].addr += objfile->section_offsets[sectindex];
  214. }
  215. return sap;
  216. }
  217. /* Initialize OBJFILE's sect_index_* members. */
  218. static void
  219. init_objfile_sect_indices (struct objfile *objfile)
  220. {
  221. asection *sect;
  222. int i;
  223. sect = bfd_get_section_by_name (objfile->obfd, ".text");
  224. if (sect)
  225. objfile->sect_index_text = sect->index;
  226. sect = bfd_get_section_by_name (objfile->obfd, ".data");
  227. if (sect)
  228. objfile->sect_index_data = sect->index;
  229. sect = bfd_get_section_by_name (objfile->obfd, ".bss");
  230. if (sect)
  231. objfile->sect_index_bss = sect->index;
  232. sect = bfd_get_section_by_name (objfile->obfd, ".rodata");
  233. if (sect)
  234. objfile->sect_index_rodata = sect->index;
  235. /* This is where things get really weird... We MUST have valid
  236. indices for the various sect_index_* members or gdb will abort.
  237. So if for example, there is no ".text" section, we have to
  238. accomodate that. First, check for a file with the standard
  239. one or two segments. */
  240. symfile_find_segment_sections (objfile);
  241. /* Except when explicitly adding symbol files at some address,
  242. section_offsets contains nothing but zeros, so it doesn't matter
  243. which slot in section_offsets the individual sect_index_* members
  244. index into. So if they are all zero, it is safe to just point
  245. all the currently uninitialized indices to the first slot. But
  246. beware: if this is the main executable, it may be relocated
  247. later, e.g. by the remote qOffsets packet, and then this will
  248. be wrong! That's why we try segments first. */
  249. for (i = 0; i < objfile->section_offsets.size (); i++)
  250. {
  251. if (objfile->section_offsets[i] != 0)
  252. {
  253. break;
  254. }
  255. }
  256. if (i == objfile->section_offsets.size ())
  257. {
  258. if (objfile->sect_index_text == -1)
  259. objfile->sect_index_text = 0;
  260. if (objfile->sect_index_data == -1)
  261. objfile->sect_index_data = 0;
  262. if (objfile->sect_index_bss == -1)
  263. objfile->sect_index_bss = 0;
  264. if (objfile->sect_index_rodata == -1)
  265. objfile->sect_index_rodata = 0;
  266. }
  267. }
  268. /* Find a unique offset to use for loadable section SECT if
  269. the user did not provide an offset. */
  270. static void
  271. place_section (bfd *abfd, asection *sect, section_offsets &offsets,
  272. CORE_ADDR &lowest)
  273. {
  274. CORE_ADDR start_addr;
  275. int done;
  276. ULONGEST align = ((ULONGEST) 1) << bfd_section_alignment (sect);
  277. /* We are only interested in allocated sections. */
  278. if ((bfd_section_flags (sect) & SEC_ALLOC) == 0)
  279. return;
  280. /* If the user specified an offset, honor it. */
  281. if (offsets[gdb_bfd_section_index (abfd, sect)] != 0)
  282. return;
  283. /* Otherwise, let's try to find a place for the section. */
  284. start_addr = (lowest + align - 1) & -align;
  285. do {
  286. asection *cur_sec;
  287. done = 1;
  288. for (cur_sec = abfd->sections; cur_sec != NULL; cur_sec = cur_sec->next)
  289. {
  290. int indx = cur_sec->index;
  291. /* We don't need to compare against ourself. */
  292. if (cur_sec == sect)
  293. continue;
  294. /* We can only conflict with allocated sections. */
  295. if ((bfd_section_flags (cur_sec) & SEC_ALLOC) == 0)
  296. continue;
  297. /* If the section offset is 0, either the section has not been placed
  298. yet, or it was the lowest section placed (in which case LOWEST
  299. will be past its end). */
  300. if (offsets[indx] == 0)
  301. continue;
  302. /* If this section would overlap us, then we must move up. */
  303. if (start_addr + bfd_section_size (sect) > offsets[indx]
  304. && start_addr < offsets[indx] + bfd_section_size (cur_sec))
  305. {
  306. start_addr = offsets[indx] + bfd_section_size (cur_sec);
  307. start_addr = (start_addr + align - 1) & -align;
  308. done = 0;
  309. break;
  310. }
  311. /* Otherwise, we appear to be OK. So far. */
  312. }
  313. }
  314. while (!done);
  315. offsets[gdb_bfd_section_index (abfd, sect)] = start_addr;
  316. lowest = start_addr + bfd_section_size (sect);
  317. }
  318. /* Store section_addr_info as prepared (made relative and with SECTINDEX
  319. filled-in) by addr_info_make_relative into SECTION_OFFSETS. */
  320. void
  321. relative_addr_info_to_section_offsets (section_offsets &section_offsets,
  322. const section_addr_info &addrs)
  323. {
  324. int i;
  325. section_offsets.assign (section_offsets.size (), 0);
  326. /* Now calculate offsets for section that were specified by the caller. */
  327. for (i = 0; i < addrs.size (); i++)
  328. {
  329. const struct other_sections *osp;
  330. osp = &addrs[i];
  331. if (osp->sectindex == -1)
  332. continue;
  333. /* Record all sections in offsets. */
  334. /* The section_offsets in the objfile are here filled in using
  335. the BFD index. */
  336. section_offsets[osp->sectindex] = osp->addr;
  337. }
  338. }
  339. /* Transform section name S for a name comparison. prelink can split section
  340. `.bss' into two sections `.dynbss' and `.bss' (in this order). Similarly
  341. prelink can split `.sbss' into `.sdynbss' and `.sbss'. Use virtual address
  342. of the new `.dynbss' (`.sdynbss') section as the adjacent new `.bss'
  343. (`.sbss') section has invalid (increased) virtual address. */
  344. static const char *
  345. addr_section_name (const char *s)
  346. {
  347. if (strcmp (s, ".dynbss") == 0)
  348. return ".bss";
  349. if (strcmp (s, ".sdynbss") == 0)
  350. return ".sbss";
  351. return s;
  352. }
  353. /* std::sort comparator for addrs_section_sort. Sort entries in
  354. ascending order by their (name, sectindex) pair. sectindex makes
  355. the sort by name stable. */
  356. static bool
  357. addrs_section_compar (const struct other_sections *a,
  358. const struct other_sections *b)
  359. {
  360. int retval;
  361. retval = strcmp (addr_section_name (a->name.c_str ()),
  362. addr_section_name (b->name.c_str ()));
  363. if (retval != 0)
  364. return retval < 0;
  365. return a->sectindex < b->sectindex;
  366. }
  367. /* Provide sorted array of pointers to sections of ADDRS. */
  368. static std::vector<const struct other_sections *>
  369. addrs_section_sort (const section_addr_info &addrs)
  370. {
  371. int i;
  372. std::vector<const struct other_sections *> array (addrs.size ());
  373. for (i = 0; i < addrs.size (); i++)
  374. array[i] = &addrs[i];
  375. std::sort (array.begin (), array.end (), addrs_section_compar);
  376. return array;
  377. }
  378. /* Relativize absolute addresses in ADDRS into offsets based on ABFD. Fill-in
  379. also SECTINDEXes specific to ABFD there. This function can be used to
  380. rebase ADDRS to start referencing different BFD than before. */
  381. void
  382. addr_info_make_relative (section_addr_info *addrs, bfd *abfd)
  383. {
  384. asection *lower_sect;
  385. CORE_ADDR lower_offset;
  386. int i;
  387. /* Find lowest loadable section to be used as starting point for
  388. contiguous sections. */
  389. lower_sect = NULL;
  390. for (asection *iter : gdb_bfd_sections (abfd))
  391. find_lowest_section (iter, &lower_sect);
  392. if (lower_sect == NULL)
  393. {
  394. warning (_("no loadable sections found in added symbol-file %s"),
  395. bfd_get_filename (abfd));
  396. lower_offset = 0;
  397. }
  398. else
  399. lower_offset = bfd_section_vma (lower_sect);
  400. /* Create ADDRS_TO_ABFD_ADDRS array to map the sections in ADDRS to sections
  401. in ABFD. Section names are not unique - there can be multiple sections of
  402. the same name. Also the sections of the same name do not have to be
  403. adjacent to each other. Some sections may be present only in one of the
  404. files. Even sections present in both files do not have to be in the same
  405. order.
  406. Use stable sort by name for the sections in both files. Then linearly
  407. scan both lists matching as most of the entries as possible. */
  408. std::vector<const struct other_sections *> addrs_sorted
  409. = addrs_section_sort (*addrs);
  410. section_addr_info abfd_addrs = build_section_addr_info_from_bfd (abfd);
  411. std::vector<const struct other_sections *> abfd_addrs_sorted
  412. = addrs_section_sort (abfd_addrs);
  413. /* Now create ADDRS_TO_ABFD_ADDRS from ADDRS_SORTED and
  414. ABFD_ADDRS_SORTED. */
  415. std::vector<const struct other_sections *>
  416. addrs_to_abfd_addrs (addrs->size (), nullptr);
  417. std::vector<const struct other_sections *>::iterator abfd_sorted_iter
  418. = abfd_addrs_sorted.begin ();
  419. for (const other_sections *sect : addrs_sorted)
  420. {
  421. const char *sect_name = addr_section_name (sect->name.c_str ());
  422. while (abfd_sorted_iter != abfd_addrs_sorted.end ()
  423. && strcmp (addr_section_name ((*abfd_sorted_iter)->name.c_str ()),
  424. sect_name) < 0)
  425. abfd_sorted_iter++;
  426. if (abfd_sorted_iter != abfd_addrs_sorted.end ()
  427. && strcmp (addr_section_name ((*abfd_sorted_iter)->name.c_str ()),
  428. sect_name) == 0)
  429. {
  430. int index_in_addrs;
  431. /* Make the found item directly addressable from ADDRS. */
  432. index_in_addrs = sect - addrs->data ();
  433. gdb_assert (addrs_to_abfd_addrs[index_in_addrs] == NULL);
  434. addrs_to_abfd_addrs[index_in_addrs] = *abfd_sorted_iter;
  435. /* Never use the same ABFD entry twice. */
  436. abfd_sorted_iter++;
  437. }
  438. }
  439. /* Calculate offsets for the loadable sections.
  440. FIXME! Sections must be in order of increasing loadable section
  441. so that contiguous sections can use the lower-offset!!!
  442. Adjust offsets if the segments are not contiguous.
  443. If the section is contiguous, its offset should be set to
  444. the offset of the highest loadable section lower than it
  445. (the loadable section directly below it in memory).
  446. this_offset = lower_offset = lower_addr - lower_orig_addr */
  447. for (i = 0; i < addrs->size (); i++)
  448. {
  449. const struct other_sections *sect = addrs_to_abfd_addrs[i];
  450. if (sect)
  451. {
  452. /* This is the index used by BFD. */
  453. (*addrs)[i].sectindex = sect->sectindex;
  454. if ((*addrs)[i].addr != 0)
  455. {
  456. (*addrs)[i].addr -= sect->addr;
  457. lower_offset = (*addrs)[i].addr;
  458. }
  459. else
  460. (*addrs)[i].addr = lower_offset;
  461. }
  462. else
  463. {
  464. /* addr_section_name transformation is not used for SECT_NAME. */
  465. const std::string &sect_name = (*addrs)[i].name;
  466. /* This section does not exist in ABFD, which is normally
  467. unexpected and we want to issue a warning.
  468. However, the ELF prelinker does create a few sections which are
  469. marked in the main executable as loadable (they are loaded in
  470. memory from the DYNAMIC segment) and yet are not present in
  471. separate debug info files. This is fine, and should not cause
  472. a warning. Shared libraries contain just the section
  473. ".gnu.liblist" but it is not marked as loadable there. There is
  474. no other way to identify them than by their name as the sections
  475. created by prelink have no special flags.
  476. For the sections `.bss' and `.sbss' see addr_section_name. */
  477. if (!(sect_name == ".gnu.liblist"
  478. || sect_name == ".gnu.conflict"
  479. || (sect_name == ".bss"
  480. && i > 0
  481. && (*addrs)[i - 1].name == ".dynbss"
  482. && addrs_to_abfd_addrs[i - 1] != NULL)
  483. || (sect_name == ".sbss"
  484. && i > 0
  485. && (*addrs)[i - 1].name == ".sdynbss"
  486. && addrs_to_abfd_addrs[i - 1] != NULL)))
  487. warning (_("section %s not found in %s"), sect_name.c_str (),
  488. bfd_get_filename (abfd));
  489. (*addrs)[i].addr = 0;
  490. (*addrs)[i].sectindex = -1;
  491. }
  492. }
  493. }
  494. /* Parse the user's idea of an offset for dynamic linking, into our idea
  495. of how to represent it for fast symbol reading. This is the default
  496. version of the sym_fns.sym_offsets function for symbol readers that
  497. don't need to do anything special. It allocates a section_offsets table
  498. for the objectfile OBJFILE and stuffs ADDR into all of the offsets. */
  499. void
  500. default_symfile_offsets (struct objfile *objfile,
  501. const section_addr_info &addrs)
  502. {
  503. objfile->section_offsets.resize (gdb_bfd_count_sections (objfile->obfd));
  504. relative_addr_info_to_section_offsets (objfile->section_offsets, addrs);
  505. /* For relocatable files, all loadable sections will start at zero.
  506. The zero is meaningless, so try to pick arbitrary addresses such
  507. that no loadable sections overlap. This algorithm is quadratic,
  508. but the number of sections in a single object file is generally
  509. small. */
  510. if ((bfd_get_file_flags (objfile->obfd) & (EXEC_P | DYNAMIC)) == 0)
  511. {
  512. bfd *abfd = objfile->obfd;
  513. asection *cur_sec;
  514. for (cur_sec = abfd->sections; cur_sec != NULL; cur_sec = cur_sec->next)
  515. /* We do not expect this to happen; just skip this step if the
  516. relocatable file has a section with an assigned VMA. */
  517. if (bfd_section_vma (cur_sec) != 0)
  518. break;
  519. if (cur_sec == NULL)
  520. {
  521. section_offsets &offsets = objfile->section_offsets;
  522. /* Pick non-overlapping offsets for sections the user did not
  523. place explicitly. */
  524. CORE_ADDR lowest = 0;
  525. for (asection *sect : gdb_bfd_sections (objfile->obfd))
  526. place_section (objfile->obfd, sect, objfile->section_offsets,
  527. lowest);
  528. /* Correctly filling in the section offsets is not quite
  529. enough. Relocatable files have two properties that
  530. (most) shared objects do not:
  531. - Their debug information will contain relocations. Some
  532. shared libraries do also, but many do not, so this can not
  533. be assumed.
  534. - If there are multiple code sections they will be loaded
  535. at different relative addresses in memory than they are
  536. in the objfile, since all sections in the file will start
  537. at address zero.
  538. Because GDB has very limited ability to map from an
  539. address in debug info to the correct code section,
  540. it relies on adding SECT_OFF_TEXT to things which might be
  541. code. If we clear all the section offsets, and set the
  542. section VMAs instead, then symfile_relocate_debug_section
  543. will return meaningful debug information pointing at the
  544. correct sections.
  545. GDB has too many different data structures for section
  546. addresses - a bfd, objfile, and so_list all have section
  547. tables, as does exec_ops. Some of these could probably
  548. be eliminated. */
  549. for (cur_sec = abfd->sections; cur_sec != NULL;
  550. cur_sec = cur_sec->next)
  551. {
  552. if ((bfd_section_flags (cur_sec) & SEC_ALLOC) == 0)
  553. continue;
  554. bfd_set_section_vma (cur_sec, offsets[cur_sec->index]);
  555. exec_set_section_address (bfd_get_filename (abfd),
  556. cur_sec->index,
  557. offsets[cur_sec->index]);
  558. offsets[cur_sec->index] = 0;
  559. }
  560. }
  561. }
  562. /* Remember the bfd indexes for the .text, .data, .bss and
  563. .rodata sections. */
  564. init_objfile_sect_indices (objfile);
  565. }
  566. /* Divide the file into segments, which are individual relocatable units.
  567. This is the default version of the sym_fns.sym_segments function for
  568. symbol readers that do not have an explicit representation of segments.
  569. It assumes that object files do not have segments, and fully linked
  570. files have a single segment. */
  571. symfile_segment_data_up
  572. default_symfile_segments (bfd *abfd)
  573. {
  574. int num_sections, i;
  575. asection *sect;
  576. CORE_ADDR low, high;
  577. /* Relocatable files contain enough information to position each
  578. loadable section independently; they should not be relocated
  579. in segments. */
  580. if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC)) == 0)
  581. return NULL;
  582. /* Make sure there is at least one loadable section in the file. */
  583. for (sect = abfd->sections; sect != NULL; sect = sect->next)
  584. {
  585. if ((bfd_section_flags (sect) & SEC_ALLOC) == 0)
  586. continue;
  587. break;
  588. }
  589. if (sect == NULL)
  590. return NULL;
  591. low = bfd_section_vma (sect);
  592. high = low + bfd_section_size (sect);
  593. symfile_segment_data_up data (new symfile_segment_data);
  594. num_sections = bfd_count_sections (abfd);
  595. /* All elements are initialized to 0 (map to no segment). */
  596. data->segment_info.resize (num_sections);
  597. for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
  598. {
  599. CORE_ADDR vma;
  600. if ((bfd_section_flags (sect) & SEC_ALLOC) == 0)
  601. continue;
  602. vma = bfd_section_vma (sect);
  603. if (vma < low)
  604. low = vma;
  605. if (vma + bfd_section_size (sect) > high)
  606. high = vma + bfd_section_size (sect);
  607. data->segment_info[i] = 1;
  608. }
  609. data->segments.emplace_back (low, high - low);
  610. return data;
  611. }
  612. /* This is a convenience function to call sym_read for OBJFILE and
  613. possibly force the partial symbols to be read. */
  614. static void
  615. read_symbols (struct objfile *objfile, symfile_add_flags add_flags)
  616. {
  617. (*objfile->sf->sym_read) (objfile, add_flags);
  618. objfile->per_bfd->minsyms_read = true;
  619. /* find_separate_debug_file_in_section should be called only if there is
  620. single binary with no existing separate debug info file. */
  621. if (!objfile->has_partial_symbols ()
  622. && objfile->separate_debug_objfile == NULL
  623. && objfile->separate_debug_objfile_backlink == NULL)
  624. {
  625. gdb_bfd_ref_ptr abfd (find_separate_debug_file_in_section (objfile));
  626. if (abfd != NULL)
  627. {
  628. /* find_separate_debug_file_in_section uses the same filename for the
  629. virtual section-as-bfd like the bfd filename containing the
  630. section. Therefore use also non-canonical name form for the same
  631. file containing the section. */
  632. symbol_file_add_separate (abfd.get (),
  633. bfd_get_filename (abfd.get ()),
  634. add_flags | SYMFILE_NOT_FILENAME, objfile);
  635. }
  636. }
  637. if ((add_flags & SYMFILE_NO_READ) == 0)
  638. objfile->require_partial_symbols (false);
  639. }
  640. /* Initialize entry point information for this objfile. */
  641. static void
  642. init_entry_point_info (struct objfile *objfile)
  643. {
  644. struct entry_info *ei = &objfile->per_bfd->ei;
  645. if (ei->initialized)
  646. return;
  647. ei->initialized = 1;
  648. /* Save startup file's range of PC addresses to help blockframe.c
  649. decide where the bottom of the stack is. */
  650. if (bfd_get_file_flags (objfile->obfd) & EXEC_P)
  651. {
  652. /* Executable file -- record its entry point so we'll recognize
  653. the startup file because it contains the entry point. */
  654. ei->entry_point = bfd_get_start_address (objfile->obfd);
  655. ei->entry_point_p = 1;
  656. }
  657. else if (bfd_get_file_flags (objfile->obfd) & DYNAMIC
  658. && bfd_get_start_address (objfile->obfd) != 0)
  659. {
  660. /* Some shared libraries may have entry points set and be
  661. runnable. There's no clear way to indicate this, so just check
  662. for values other than zero. */
  663. ei->entry_point = bfd_get_start_address (objfile->obfd);
  664. ei->entry_point_p = 1;
  665. }
  666. else
  667. {
  668. /* Examination of non-executable.o files. Short-circuit this stuff. */
  669. ei->entry_point_p = 0;
  670. }
  671. if (ei->entry_point_p)
  672. {
  673. struct obj_section *osect;
  674. CORE_ADDR entry_point = ei->entry_point;
  675. int found;
  676. /* Make certain that the address points at real code, and not a
  677. function descriptor. */
  678. entry_point = gdbarch_convert_from_func_ptr_addr
  679. (objfile->arch (), entry_point, current_inferior ()->top_target ());
  680. /* Remove any ISA markers, so that this matches entries in the
  681. symbol table. */
  682. ei->entry_point
  683. = gdbarch_addr_bits_remove (objfile->arch (), entry_point);
  684. found = 0;
  685. ALL_OBJFILE_OSECTIONS (objfile, osect)
  686. {
  687. struct bfd_section *sect = osect->the_bfd_section;
  688. if (entry_point >= bfd_section_vma (sect)
  689. && entry_point < (bfd_section_vma (sect)
  690. + bfd_section_size (sect)))
  691. {
  692. ei->the_bfd_section_index
  693. = gdb_bfd_section_index (objfile->obfd, sect);
  694. found = 1;
  695. break;
  696. }
  697. }
  698. if (!found)
  699. ei->the_bfd_section_index = SECT_OFF_TEXT (objfile);
  700. }
  701. }
  702. /* Process a symbol file, as either the main file or as a dynamically
  703. loaded file.
  704. This function does not set the OBJFILE's entry-point info.
  705. OBJFILE is where the symbols are to be read from.
  706. ADDRS is the list of section load addresses. If the user has given
  707. an 'add-symbol-file' command, then this is the list of offsets and
  708. addresses he or she provided as arguments to the command; or, if
  709. we're handling a shared library, these are the actual addresses the
  710. sections are loaded at, according to the inferior's dynamic linker
  711. (as gleaned by GDB's shared library code). We convert each address
  712. into an offset from the section VMA's as it appears in the object
  713. file, and then call the file's sym_offsets function to convert this
  714. into a format-specific offset table --- a `section_offsets'.
  715. The sectindex field is used to control the ordering of sections
  716. with the same name. Upon return, it is updated to contain the
  717. corresponding BFD section index, or -1 if the section was not found.
  718. ADD_FLAGS encodes verbosity level, whether this is main symbol or
  719. an extra symbol file such as dynamically loaded code, and whether
  720. breakpoint reset should be deferred. */
  721. static void
  722. syms_from_objfile_1 (struct objfile *objfile,
  723. section_addr_info *addrs,
  724. symfile_add_flags add_flags)
  725. {
  726. section_addr_info local_addr;
  727. const int mainline = add_flags & SYMFILE_MAINLINE;
  728. objfile_set_sym_fns (objfile, find_sym_fns (objfile->obfd));
  729. objfile->qf.clear ();
  730. if (objfile->sf == NULL)
  731. {
  732. /* No symbols to load, but we still need to make sure
  733. that the section_offsets table is allocated. */
  734. int num_sections = gdb_bfd_count_sections (objfile->obfd);
  735. objfile->section_offsets.assign (num_sections, 0);
  736. return;
  737. }
  738. /* Make sure that partially constructed symbol tables will be cleaned up
  739. if an error occurs during symbol reading. */
  740. gdb::optional<clear_symtab_users_cleanup> defer_clear_users;
  741. objfile_up objfile_holder (objfile);
  742. /* If ADDRS is NULL, put together a dummy address list.
  743. We now establish the convention that an addr of zero means
  744. no load address was specified. */
  745. if (! addrs)
  746. addrs = &local_addr;
  747. if (mainline)
  748. {
  749. /* We will modify the main symbol table, make sure that all its users
  750. will be cleaned up if an error occurs during symbol reading. */
  751. defer_clear_users.emplace ((symfile_add_flag) 0);
  752. /* Since no error yet, throw away the old symbol table. */
  753. if (current_program_space->symfile_object_file != NULL)
  754. {
  755. current_program_space->symfile_object_file->unlink ();
  756. gdb_assert (current_program_space->symfile_object_file == NULL);
  757. }
  758. /* Currently we keep symbols from the add-symbol-file command.
  759. If the user wants to get rid of them, they should do "symbol-file"
  760. without arguments first. Not sure this is the best behavior
  761. (PR 2207). */
  762. (*objfile->sf->sym_new_init) (objfile);
  763. }
  764. /* Convert addr into an offset rather than an absolute address.
  765. We find the lowest address of a loaded segment in the objfile,
  766. and assume that <addr> is where that got loaded.
  767. We no longer warn if the lowest section is not a text segment (as
  768. happens for the PA64 port. */
  769. if (addrs->size () > 0)
  770. addr_info_make_relative (addrs, objfile->obfd);
  771. /* Initialize symbol reading routines for this objfile, allow complaints to
  772. appear for this new file, and record how verbose to be, then do the
  773. initial symbol reading for this file. */
  774. (*objfile->sf->sym_init) (objfile);
  775. clear_complaints ();
  776. (*objfile->sf->sym_offsets) (objfile, *addrs);
  777. read_symbols (objfile, add_flags);
  778. /* Discard cleanups as symbol reading was successful. */
  779. objfile_holder.release ();
  780. if (defer_clear_users)
  781. defer_clear_users->release ();
  782. }
  783. /* Same as syms_from_objfile_1, but also initializes the objfile
  784. entry-point info. */
  785. static void
  786. syms_from_objfile (struct objfile *objfile,
  787. section_addr_info *addrs,
  788. symfile_add_flags add_flags)
  789. {
  790. syms_from_objfile_1 (objfile, addrs, add_flags);
  791. init_entry_point_info (objfile);
  792. }
  793. /* Perform required actions after either reading in the initial
  794. symbols for a new objfile, or mapping in the symbols from a reusable
  795. objfile. ADD_FLAGS is a bitmask of enum symfile_add_flags. */
  796. static void
  797. finish_new_objfile (struct objfile *objfile, symfile_add_flags add_flags)
  798. {
  799. /* If this is the main symbol file we have to clean up all users of the
  800. old main symbol file. Otherwise it is sufficient to fixup all the
  801. breakpoints that may have been redefined by this symbol file. */
  802. if (add_flags & SYMFILE_MAINLINE)
  803. {
  804. /* OK, make it the "real" symbol file. */
  805. current_program_space->symfile_object_file = objfile;
  806. clear_symtab_users (add_flags);
  807. }
  808. else if ((add_flags & SYMFILE_DEFER_BP_RESET) == 0)
  809. {
  810. breakpoint_re_set ();
  811. }
  812. /* We're done reading the symbol file; finish off complaints. */
  813. clear_complaints ();
  814. }
  815. /* Process a symbol file, as either the main file or as a dynamically
  816. loaded file.
  817. ABFD is a BFD already open on the file, as from symfile_bfd_open.
  818. A new reference is acquired by this function.
  819. For NAME description see the objfile constructor.
  820. ADD_FLAGS encodes verbosity, whether this is main symbol file or
  821. extra, such as dynamically loaded code, and what to do with breakpoints.
  822. ADDRS is as described for syms_from_objfile_1, above.
  823. ADDRS is ignored when SYMFILE_MAINLINE bit is set in ADD_FLAGS.
  824. PARENT is the original objfile if ABFD is a separate debug info file.
  825. Otherwise PARENT is NULL.
  826. Upon success, returns a pointer to the objfile that was added.
  827. Upon failure, jumps back to command level (never returns). */
  828. static struct objfile *
  829. symbol_file_add_with_addrs (bfd *abfd, const char *name,
  830. symfile_add_flags add_flags,
  831. section_addr_info *addrs,
  832. objfile_flags flags, struct objfile *parent)
  833. {
  834. struct objfile *objfile;
  835. const int from_tty = add_flags & SYMFILE_VERBOSE;
  836. const int mainline = add_flags & SYMFILE_MAINLINE;
  837. const int always_confirm = add_flags & SYMFILE_ALWAYS_CONFIRM;
  838. const int should_print = (print_symbol_loading_p (from_tty, mainline, 1)
  839. && (readnow_symbol_files
  840. || (add_flags & SYMFILE_NO_READ) == 0));
  841. if (readnow_symbol_files)
  842. {
  843. flags |= OBJF_READNOW;
  844. add_flags &= ~SYMFILE_NO_READ;
  845. }
  846. else if (readnever_symbol_files
  847. || (parent != NULL && (parent->flags & OBJF_READNEVER)))
  848. {
  849. flags |= OBJF_READNEVER;
  850. add_flags |= SYMFILE_NO_READ;
  851. }
  852. if ((add_flags & SYMFILE_NOT_FILENAME) != 0)
  853. flags |= OBJF_NOT_FILENAME;
  854. /* Give user a chance to burp if ALWAYS_CONFIRM or we'd be
  855. interactively wiping out any existing symbols. */
  856. if (from_tty
  857. && (always_confirm
  858. || ((have_full_symbols () || have_partial_symbols ())
  859. && mainline))
  860. && !query (_("Load new symbol table from \"%s\"? "), name))
  861. error (_("Not confirmed."));
  862. if (mainline)
  863. flags |= OBJF_MAINLINE;
  864. objfile = objfile::make (abfd, name, flags, parent);
  865. /* We either created a new mapped symbol table, mapped an existing
  866. symbol table file which has not had initial symbol reading
  867. performed, or need to read an unmapped symbol table. */
  868. if (should_print)
  869. {
  870. if (deprecated_pre_add_symbol_hook)
  871. deprecated_pre_add_symbol_hook (name);
  872. else
  873. gdb_printf (_("Reading symbols from %ps...\n"),
  874. styled_string (file_name_style.style (), name));
  875. }
  876. syms_from_objfile (objfile, addrs, add_flags);
  877. /* We now have at least a partial symbol table. Check to see if the
  878. user requested that all symbols be read on initial access via either
  879. the gdb startup command line or on a per symbol file basis. Expand
  880. all partial symbol tables for this objfile if so. */
  881. if ((flags & OBJF_READNOW))
  882. {
  883. if (should_print)
  884. gdb_printf (_("Expanding full symbols from %ps...\n"),
  885. styled_string (file_name_style.style (), name));
  886. objfile->expand_all_symtabs ();
  887. }
  888. /* Note that we only print a message if we have no symbols and have
  889. no separate debug file. If there is a separate debug file which
  890. does not have symbols, we'll have emitted this message for that
  891. file, and so printing it twice is just redundant. */
  892. if (should_print && !objfile_has_symbols (objfile)
  893. && objfile->separate_debug_objfile == nullptr)
  894. gdb_printf (_("(No debugging symbols found in %ps)\n"),
  895. styled_string (file_name_style.style (), name));
  896. if (should_print)
  897. {
  898. if (deprecated_post_add_symbol_hook)
  899. deprecated_post_add_symbol_hook ();
  900. }
  901. /* We print some messages regardless of whether 'from_tty ||
  902. info_verbose' is true, so make sure they go out at the right
  903. time. */
  904. gdb_flush (gdb_stdout);
  905. if (objfile->sf == NULL)
  906. {
  907. gdb::observers::new_objfile.notify (objfile);
  908. return objfile; /* No symbols. */
  909. }
  910. finish_new_objfile (objfile, add_flags);
  911. gdb::observers::new_objfile.notify (objfile);
  912. bfd_cache_close_all ();
  913. return (objfile);
  914. }
  915. /* Add BFD as a separate debug file for OBJFILE. For NAME description
  916. see the objfile constructor. */
  917. void
  918. symbol_file_add_separate (bfd *bfd, const char *name,
  919. symfile_add_flags symfile_flags,
  920. struct objfile *objfile)
  921. {
  922. /* Create section_addr_info. We can't directly use offsets from OBJFILE
  923. because sections of BFD may not match sections of OBJFILE and because
  924. vma may have been modified by tools such as prelink. */
  925. section_addr_info sap = build_section_addr_info_from_objfile (objfile);
  926. symbol_file_add_with_addrs
  927. (bfd, name, symfile_flags, &sap,
  928. objfile->flags & (OBJF_REORDERED | OBJF_SHARED | OBJF_READNOW
  929. | OBJF_USERLOADED | OBJF_MAINLINE),
  930. objfile);
  931. }
  932. /* Process the symbol file ABFD, as either the main file or as a
  933. dynamically loaded file.
  934. See symbol_file_add_with_addrs's comments for details. */
  935. struct objfile *
  936. symbol_file_add_from_bfd (bfd *abfd, const char *name,
  937. symfile_add_flags add_flags,
  938. section_addr_info *addrs,
  939. objfile_flags flags, struct objfile *parent)
  940. {
  941. return symbol_file_add_with_addrs (abfd, name, add_flags, addrs, flags,
  942. parent);
  943. }
  944. /* Process a symbol file, as either the main file or as a dynamically
  945. loaded file. See symbol_file_add_with_addrs's comments for details. */
  946. struct objfile *
  947. symbol_file_add (const char *name, symfile_add_flags add_flags,
  948. section_addr_info *addrs, objfile_flags flags)
  949. {
  950. gdb_bfd_ref_ptr bfd (symfile_bfd_open (name));
  951. return symbol_file_add_from_bfd (bfd.get (), name, add_flags, addrs,
  952. flags, NULL);
  953. }
  954. /* Call symbol_file_add() with default values and update whatever is
  955. affected by the loading of a new main().
  956. Used when the file is supplied in the gdb command line
  957. and by some targets with special loading requirements.
  958. The auxiliary function, symbol_file_add_main_1(), has the flags
  959. argument for the switches that can only be specified in the symbol_file
  960. command itself. */
  961. void
  962. symbol_file_add_main (const char *args, symfile_add_flags add_flags)
  963. {
  964. symbol_file_add_main_1 (args, add_flags, 0, 0);
  965. }
  966. static void
  967. symbol_file_add_main_1 (const char *args, symfile_add_flags add_flags,
  968. objfile_flags flags, CORE_ADDR reloff)
  969. {
  970. add_flags |= current_inferior ()->symfile_flags | SYMFILE_MAINLINE;
  971. struct objfile *objfile = symbol_file_add (args, add_flags, NULL, flags);
  972. if (reloff != 0)
  973. objfile_rebase (objfile, reloff);
  974. /* Getting new symbols may change our opinion about
  975. what is frameless. */
  976. reinit_frame_cache ();
  977. if ((add_flags & SYMFILE_NO_READ) == 0)
  978. set_initial_language ();
  979. }
  980. void
  981. symbol_file_clear (int from_tty)
  982. {
  983. if ((have_full_symbols () || have_partial_symbols ())
  984. && from_tty
  985. && (current_program_space->symfile_object_file
  986. ? !query (_("Discard symbol table from `%s'? "),
  987. objfile_name (current_program_space->symfile_object_file))
  988. : !query (_("Discard symbol table? "))))
  989. error (_("Not confirmed."));
  990. /* solib descriptors may have handles to objfiles. Wipe them before their
  991. objfiles get stale by free_all_objfiles. */
  992. no_shared_libraries (NULL, from_tty);
  993. current_program_space->free_all_objfiles ();
  994. clear_symtab_users (0);
  995. gdb_assert (current_program_space->symfile_object_file == NULL);
  996. if (from_tty)
  997. gdb_printf (_("No symbol file now.\n"));
  998. }
  999. /* See symfile.h. */
  1000. bool separate_debug_file_debug = false;
  1001. static int
  1002. separate_debug_file_exists (const std::string &name, unsigned long crc,
  1003. struct objfile *parent_objfile)
  1004. {
  1005. unsigned long file_crc;
  1006. int file_crc_p;
  1007. struct stat parent_stat, abfd_stat;
  1008. int verified_as_different;
  1009. /* Find a separate debug info file as if symbols would be present in
  1010. PARENT_OBJFILE itself this function would not be called. .gnu_debuglink
  1011. section can contain just the basename of PARENT_OBJFILE without any
  1012. ".debug" suffix as "/usr/lib/debug/path/to/file" is a separate tree where
  1013. the separate debug infos with the same basename can exist. */
  1014. if (filename_cmp (name.c_str (), objfile_name (parent_objfile)) == 0)
  1015. return 0;
  1016. if (separate_debug_file_debug)
  1017. {
  1018. gdb_printf (gdb_stdlog, _(" Trying %s..."), name.c_str ());
  1019. gdb_flush (gdb_stdlog);
  1020. }
  1021. gdb_bfd_ref_ptr abfd (gdb_bfd_open (name.c_str (), gnutarget));
  1022. if (abfd == NULL)
  1023. {
  1024. if (separate_debug_file_debug)
  1025. gdb_printf (gdb_stdlog, _(" no, unable to open.\n"));
  1026. return 0;
  1027. }
  1028. /* Verify symlinks were not the cause of filename_cmp name difference above.
  1029. Some operating systems, e.g. Windows, do not provide a meaningful
  1030. st_ino; they always set it to zero. (Windows does provide a
  1031. meaningful st_dev.) Files accessed from gdbservers that do not
  1032. support the vFile:fstat packet will also have st_ino set to zero.
  1033. Do not indicate a duplicate library in either case. While there
  1034. is no guarantee that a system that provides meaningful inode
  1035. numbers will never set st_ino to zero, this is merely an
  1036. optimization, so we do not need to worry about false negatives. */
  1037. if (bfd_stat (abfd.get (), &abfd_stat) == 0
  1038. && abfd_stat.st_ino != 0
  1039. && bfd_stat (parent_objfile->obfd, &parent_stat) == 0)
  1040. {
  1041. if (abfd_stat.st_dev == parent_stat.st_dev
  1042. && abfd_stat.st_ino == parent_stat.st_ino)
  1043. {
  1044. if (separate_debug_file_debug)
  1045. gdb_printf (gdb_stdlog,
  1046. _(" no, same file as the objfile.\n"));
  1047. return 0;
  1048. }
  1049. verified_as_different = 1;
  1050. }
  1051. else
  1052. verified_as_different = 0;
  1053. file_crc_p = gdb_bfd_crc (abfd.get (), &file_crc);
  1054. if (!file_crc_p)
  1055. {
  1056. if (separate_debug_file_debug)
  1057. gdb_printf (gdb_stdlog, _(" no, error computing CRC.\n"));
  1058. return 0;
  1059. }
  1060. if (crc != file_crc)
  1061. {
  1062. unsigned long parent_crc;
  1063. /* If the files could not be verified as different with
  1064. bfd_stat then we need to calculate the parent's CRC
  1065. to verify whether the files are different or not. */
  1066. if (!verified_as_different)
  1067. {
  1068. if (!gdb_bfd_crc (parent_objfile->obfd, &parent_crc))
  1069. {
  1070. if (separate_debug_file_debug)
  1071. gdb_printf (gdb_stdlog,
  1072. _(" no, error computing CRC.\n"));
  1073. return 0;
  1074. }
  1075. }
  1076. if (verified_as_different || parent_crc != file_crc)
  1077. warning (_("the debug information found in \"%s\""
  1078. " does not match \"%s\" (CRC mismatch).\n"),
  1079. name.c_str (), objfile_name (parent_objfile));
  1080. if (separate_debug_file_debug)
  1081. gdb_printf (gdb_stdlog, _(" no, CRC doesn't match.\n"));
  1082. return 0;
  1083. }
  1084. if (separate_debug_file_debug)
  1085. gdb_printf (gdb_stdlog, _(" yes!\n"));
  1086. return 1;
  1087. }
  1088. std::string debug_file_directory;
  1089. static void
  1090. show_debug_file_directory (struct ui_file *file, int from_tty,
  1091. struct cmd_list_element *c, const char *value)
  1092. {
  1093. gdb_printf (file,
  1094. _("The directory where separate debug "
  1095. "symbols are searched for is \"%s\".\n"),
  1096. value);
  1097. }
  1098. #if ! defined (DEBUG_SUBDIRECTORY)
  1099. #define DEBUG_SUBDIRECTORY ".debug"
  1100. #endif
  1101. /* Find a separate debuginfo file for OBJFILE, using DIR as the directory
  1102. where the original file resides (may not be the same as
  1103. dirname(objfile->name) due to symlinks), and DEBUGLINK as the file we are
  1104. looking for. CANON_DIR is the "realpath" form of DIR.
  1105. DIR must contain a trailing '/'.
  1106. Returns the path of the file with separate debug info, or an empty
  1107. string. */
  1108. static std::string
  1109. find_separate_debug_file (const char *dir,
  1110. const char *canon_dir,
  1111. const char *debuglink,
  1112. unsigned long crc32, struct objfile *objfile)
  1113. {
  1114. if (separate_debug_file_debug)
  1115. gdb_printf (gdb_stdlog,
  1116. _("\nLooking for separate debug info (debug link) for "
  1117. "%s\n"), objfile_name (objfile));
  1118. /* First try in the same directory as the original file. */
  1119. std::string debugfile = dir;
  1120. debugfile += debuglink;
  1121. if (separate_debug_file_exists (debugfile, crc32, objfile))
  1122. return debugfile;
  1123. /* Then try in the subdirectory named DEBUG_SUBDIRECTORY. */
  1124. debugfile = dir;
  1125. debugfile += DEBUG_SUBDIRECTORY;
  1126. debugfile += "/";
  1127. debugfile += debuglink;
  1128. if (separate_debug_file_exists (debugfile, crc32, objfile))
  1129. return debugfile;
  1130. /* Then try in the global debugfile directories.
  1131. Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
  1132. cause "/..." lookups. */
  1133. bool target_prefix = startswith (dir, "target:");
  1134. const char *dir_notarget = target_prefix ? dir + strlen ("target:") : dir;
  1135. std::vector<gdb::unique_xmalloc_ptr<char>> debugdir_vec
  1136. = dirnames_to_char_ptr_vec (debug_file_directory.c_str ());
  1137. gdb::unique_xmalloc_ptr<char> canon_sysroot
  1138. = gdb_realpath (gdb_sysroot.c_str ());
  1139. /* MS-Windows/MS-DOS don't allow colons in file names; we must
  1140. convert the drive letter into a one-letter directory, so that the
  1141. file name resulting from splicing below will be valid.
  1142. FIXME: The below only works when GDB runs on MS-Windows/MS-DOS.
  1143. There are various remote-debugging scenarios where such a
  1144. transformation of the drive letter might be required when GDB runs
  1145. on a Posix host, see
  1146. https://sourceware.org/ml/gdb-patches/2019-04/msg00605.html
  1147. If some of those scenarios need to be supported, we will need to
  1148. use a different condition for HAS_DRIVE_SPEC and a different macro
  1149. instead of STRIP_DRIVE_SPEC, which work on Posix systems as well. */
  1150. std::string drive;
  1151. if (HAS_DRIVE_SPEC (dir_notarget))
  1152. {
  1153. drive = dir_notarget[0];
  1154. dir_notarget = STRIP_DRIVE_SPEC (dir_notarget);
  1155. }
  1156. for (const gdb::unique_xmalloc_ptr<char> &debugdir : debugdir_vec)
  1157. {
  1158. debugfile = target_prefix ? "target:" : "";
  1159. debugfile += debugdir;
  1160. debugfile += "/";
  1161. debugfile += drive;
  1162. debugfile += dir_notarget;
  1163. debugfile += debuglink;
  1164. if (separate_debug_file_exists (debugfile, crc32, objfile))
  1165. return debugfile;
  1166. const char *base_path = NULL;
  1167. if (canon_dir != NULL)
  1168. {
  1169. if (canon_sysroot.get () != NULL)
  1170. base_path = child_path (canon_sysroot.get (), canon_dir);
  1171. else
  1172. base_path = child_path (gdb_sysroot.c_str (), canon_dir);
  1173. }
  1174. if (base_path != NULL)
  1175. {
  1176. /* If the file is in the sysroot, try using its base path in
  1177. the global debugfile directory. */
  1178. debugfile = target_prefix ? "target:" : "";
  1179. debugfile += debugdir;
  1180. debugfile += "/";
  1181. debugfile += base_path;
  1182. debugfile += "/";
  1183. debugfile += debuglink;
  1184. if (separate_debug_file_exists (debugfile, crc32, objfile))
  1185. return debugfile;
  1186. /* If the file is in the sysroot, try using its base path in
  1187. the sysroot's global debugfile directory. */
  1188. debugfile = target_prefix ? "target:" : "";
  1189. debugfile += gdb_sysroot;
  1190. debugfile += debugdir;
  1191. debugfile += "/";
  1192. debugfile += base_path;
  1193. debugfile += "/";
  1194. debugfile += debuglink;
  1195. if (separate_debug_file_exists (debugfile, crc32, objfile))
  1196. return debugfile;
  1197. }
  1198. }
  1199. return std::string ();
  1200. }
  1201. /* Modify PATH to contain only "[/]directory/" part of PATH.
  1202. If there were no directory separators in PATH, PATH will be empty
  1203. string on return. */
  1204. static void
  1205. terminate_after_last_dir_separator (char *path)
  1206. {
  1207. int i;
  1208. /* Strip off the final filename part, leaving the directory name,
  1209. followed by a slash. The directory can be relative or absolute. */
  1210. for (i = strlen(path) - 1; i >= 0; i--)
  1211. if (IS_DIR_SEPARATOR (path[i]))
  1212. break;
  1213. /* If I is -1 then no directory is present there and DIR will be "". */
  1214. path[i + 1] = '\0';
  1215. }
  1216. /* Find separate debuginfo for OBJFILE (using .gnu_debuglink section).
  1217. Returns pathname, or an empty string. */
  1218. std::string
  1219. find_separate_debug_file_by_debuglink (struct objfile *objfile)
  1220. {
  1221. unsigned long crc32;
  1222. gdb::unique_xmalloc_ptr<char> debuglink
  1223. (bfd_get_debug_link_info (objfile->obfd, &crc32));
  1224. if (debuglink == NULL)
  1225. {
  1226. /* There's no separate debug info, hence there's no way we could
  1227. load it => no warning. */
  1228. return std::string ();
  1229. }
  1230. std::string dir = objfile_name (objfile);
  1231. terminate_after_last_dir_separator (&dir[0]);
  1232. gdb::unique_xmalloc_ptr<char> canon_dir (lrealpath (dir.c_str ()));
  1233. std::string debugfile
  1234. = find_separate_debug_file (dir.c_str (), canon_dir.get (),
  1235. debuglink.get (), crc32, objfile);
  1236. if (debugfile.empty ())
  1237. {
  1238. /* For PR gdb/9538, try again with realpath (if different from the
  1239. original). */
  1240. struct stat st_buf;
  1241. if (lstat (objfile_name (objfile), &st_buf) == 0
  1242. && S_ISLNK (st_buf.st_mode))
  1243. {
  1244. gdb::unique_xmalloc_ptr<char> symlink_dir
  1245. (lrealpath (objfile_name (objfile)));
  1246. if (symlink_dir != NULL)
  1247. {
  1248. terminate_after_last_dir_separator (symlink_dir.get ());
  1249. if (dir != symlink_dir.get ())
  1250. {
  1251. /* Different directory, so try using it. */
  1252. debugfile = find_separate_debug_file (symlink_dir.get (),
  1253. symlink_dir.get (),
  1254. debuglink.get (),
  1255. crc32,
  1256. objfile);
  1257. }
  1258. }
  1259. }
  1260. }
  1261. return debugfile;
  1262. }
  1263. /* Make sure that OBJF_{READNOW,READNEVER} are not set
  1264. simultaneously. */
  1265. static void
  1266. validate_readnow_readnever (objfile_flags flags)
  1267. {
  1268. if ((flags & OBJF_READNOW) && (flags & OBJF_READNEVER))
  1269. error (_("-readnow and -readnever cannot be used simultaneously"));
  1270. }
  1271. /* This is the symbol-file command. Read the file, analyze its
  1272. symbols, and add a struct symtab to a symtab list. The syntax of
  1273. the command is rather bizarre:
  1274. 1. The function buildargv implements various quoting conventions
  1275. which are undocumented and have little or nothing in common with
  1276. the way things are quoted (or not quoted) elsewhere in GDB.
  1277. 2. Options are used, which are not generally used in GDB (perhaps
  1278. "set mapped on", "set readnow on" would be better)
  1279. 3. The order of options matters, which is contrary to GNU
  1280. conventions (because it is confusing and inconvenient). */
  1281. void
  1282. symbol_file_command (const char *args, int from_tty)
  1283. {
  1284. dont_repeat ();
  1285. if (args == NULL)
  1286. {
  1287. symbol_file_clear (from_tty);
  1288. }
  1289. else
  1290. {
  1291. objfile_flags flags = OBJF_USERLOADED;
  1292. symfile_add_flags add_flags = 0;
  1293. char *name = NULL;
  1294. bool stop_processing_options = false;
  1295. CORE_ADDR offset = 0;
  1296. int idx;
  1297. char *arg;
  1298. if (from_tty)
  1299. add_flags |= SYMFILE_VERBOSE;
  1300. gdb_argv built_argv (args);
  1301. for (arg = built_argv[0], idx = 0; arg != NULL; arg = built_argv[++idx])
  1302. {
  1303. if (stop_processing_options || *arg != '-')
  1304. {
  1305. if (name == NULL)
  1306. name = arg;
  1307. else
  1308. error (_("Unrecognized argument \"%s\""), arg);
  1309. }
  1310. else if (strcmp (arg, "-readnow") == 0)
  1311. flags |= OBJF_READNOW;
  1312. else if (strcmp (arg, "-readnever") == 0)
  1313. flags |= OBJF_READNEVER;
  1314. else if (strcmp (arg, "-o") == 0)
  1315. {
  1316. arg = built_argv[++idx];
  1317. if (arg == NULL)
  1318. error (_("Missing argument to -o"));
  1319. offset = parse_and_eval_address (arg);
  1320. }
  1321. else if (strcmp (arg, "--") == 0)
  1322. stop_processing_options = true;
  1323. else
  1324. error (_("Unrecognized argument \"%s\""), arg);
  1325. }
  1326. if (name == NULL)
  1327. error (_("no symbol file name was specified"));
  1328. validate_readnow_readnever (flags);
  1329. /* Set SYMFILE_DEFER_BP_RESET because the proper displacement for a PIE
  1330. (Position Independent Executable) main symbol file will only be
  1331. computed by the solib_create_inferior_hook below. Without it,
  1332. breakpoint_re_set would fail to insert the breakpoints with the zero
  1333. displacement. */
  1334. add_flags |= SYMFILE_DEFER_BP_RESET;
  1335. symbol_file_add_main_1 (name, add_flags, flags, offset);
  1336. solib_create_inferior_hook (from_tty);
  1337. /* Now it's safe to re-add the breakpoints. */
  1338. breakpoint_re_set ();
  1339. }
  1340. }
  1341. /* Set the initial language. */
  1342. void
  1343. set_initial_language (void)
  1344. {
  1345. if (language_mode == language_mode_manual)
  1346. return;
  1347. enum language lang = main_language ();
  1348. /* Make C the default language. */
  1349. enum language default_lang = language_c;
  1350. if (lang == language_unknown)
  1351. {
  1352. const char *name = main_name ();
  1353. struct symbol *sym
  1354. = lookup_symbol_in_language (name, NULL, VAR_DOMAIN, default_lang,
  1355. NULL).symbol;
  1356. if (sym != NULL)
  1357. lang = sym->language ();
  1358. }
  1359. if (lang == language_unknown)
  1360. {
  1361. lang = default_lang;
  1362. }
  1363. set_language (lang);
  1364. expected_language = current_language; /* Don't warn the user. */
  1365. }
  1366. /* Open the file specified by NAME and hand it off to BFD for
  1367. preliminary analysis. Return a newly initialized bfd *, which
  1368. includes a newly malloc'd` copy of NAME (tilde-expanded and made
  1369. absolute). In case of trouble, error() is called. */
  1370. gdb_bfd_ref_ptr
  1371. symfile_bfd_open (const char *name)
  1372. {
  1373. int desc = -1;
  1374. gdb::unique_xmalloc_ptr<char> absolute_name;
  1375. if (!is_target_filename (name))
  1376. {
  1377. gdb::unique_xmalloc_ptr<char> expanded_name (tilde_expand (name));
  1378. /* Look down path for it, allocate 2nd new malloc'd copy. */
  1379. desc = openp (getenv ("PATH"),
  1380. OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH,
  1381. expanded_name.get (), O_RDONLY | O_BINARY, &absolute_name);
  1382. #if defined(__GO32__) || defined(_WIN32) || defined (__CYGWIN__)
  1383. if (desc < 0)
  1384. {
  1385. char *exename = (char *) alloca (strlen (expanded_name.get ()) + 5);
  1386. strcat (strcpy (exename, expanded_name.get ()), ".exe");
  1387. desc = openp (getenv ("PATH"),
  1388. OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH,
  1389. exename, O_RDONLY | O_BINARY, &absolute_name);
  1390. }
  1391. #endif
  1392. if (desc < 0)
  1393. perror_with_name (expanded_name.get ());
  1394. name = absolute_name.get ();
  1395. }
  1396. gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (name, gnutarget, desc));
  1397. if (sym_bfd == NULL)
  1398. error (_("`%s': can't open to read symbols: %s."), name,
  1399. bfd_errmsg (bfd_get_error ()));
  1400. if (!gdb_bfd_has_target_filename (sym_bfd.get ()))
  1401. bfd_set_cacheable (sym_bfd.get (), 1);
  1402. if (!bfd_check_format (sym_bfd.get (), bfd_object))
  1403. error (_("`%s': can't read symbols: %s."), name,
  1404. bfd_errmsg (bfd_get_error ()));
  1405. return sym_bfd;
  1406. }
  1407. /* Return the section index for SECTION_NAME on OBJFILE. Return -1 if
  1408. the section was not found. */
  1409. int
  1410. get_section_index (struct objfile *objfile, const char *section_name)
  1411. {
  1412. asection *sect = bfd_get_section_by_name (objfile->obfd, section_name);
  1413. if (sect)
  1414. return sect->index;
  1415. else
  1416. return -1;
  1417. }
  1418. /* Link SF into the global symtab_fns list.
  1419. FLAVOUR is the file format that SF handles.
  1420. Called on startup by the _initialize routine in each object file format
  1421. reader, to register information about each format the reader is prepared
  1422. to handle. */
  1423. void
  1424. add_symtab_fns (enum bfd_flavour flavour, const struct sym_fns *sf)
  1425. {
  1426. symtab_fns.emplace_back (flavour, sf);
  1427. }
  1428. /* Initialize OBJFILE to read symbols from its associated BFD. It
  1429. either returns or calls error(). The result is an initialized
  1430. struct sym_fns in the objfile structure, that contains cached
  1431. information about the symbol file. */
  1432. static const struct sym_fns *
  1433. find_sym_fns (bfd *abfd)
  1434. {
  1435. enum bfd_flavour our_flavour = bfd_get_flavour (abfd);
  1436. if (our_flavour == bfd_target_srec_flavour
  1437. || our_flavour == bfd_target_ihex_flavour
  1438. || our_flavour == bfd_target_tekhex_flavour)
  1439. return NULL; /* No symbols. */
  1440. for (const registered_sym_fns &rsf : symtab_fns)
  1441. if (our_flavour == rsf.sym_flavour)
  1442. return rsf.sym_fns;
  1443. error (_("I'm sorry, Dave, I can't do that. Symbol format `%s' unknown."),
  1444. bfd_get_target (abfd));
  1445. }
  1446. /* This function runs the load command of our current target. */
  1447. static void
  1448. load_command (const char *arg, int from_tty)
  1449. {
  1450. dont_repeat ();
  1451. /* The user might be reloading because the binary has changed. Take
  1452. this opportunity to check. */
  1453. reopen_exec_file ();
  1454. reread_symbols (from_tty);
  1455. std::string temp;
  1456. if (arg == NULL)
  1457. {
  1458. const char *parg, *prev;
  1459. arg = get_exec_file (1);
  1460. /* We may need to quote this string so buildargv can pull it
  1461. apart. */
  1462. prev = parg = arg;
  1463. while ((parg = strpbrk (parg, "\\\"'\t ")))
  1464. {
  1465. temp.append (prev, parg - prev);
  1466. prev = parg++;
  1467. temp.push_back ('\\');
  1468. }
  1469. /* If we have not copied anything yet, then we didn't see a
  1470. character to quote, and we can just leave ARG unchanged. */
  1471. if (!temp.empty ())
  1472. {
  1473. temp.append (prev);
  1474. arg = temp.c_str ();
  1475. }
  1476. }
  1477. target_load (arg, from_tty);
  1478. /* After re-loading the executable, we don't really know which
  1479. overlays are mapped any more. */
  1480. overlay_cache_invalid = 1;
  1481. }
  1482. /* This version of "load" should be usable for any target. Currently
  1483. it is just used for remote targets, not inftarg.c or core files,
  1484. on the theory that only in that case is it useful.
  1485. Avoiding xmodem and the like seems like a win (a) because we don't have
  1486. to worry about finding it, and (b) On VMS, fork() is very slow and so
  1487. we don't want to run a subprocess. On the other hand, I'm not sure how
  1488. performance compares. */
  1489. static int validate_download = 0;
  1490. /* Opaque data for load_progress. */
  1491. struct load_progress_data
  1492. {
  1493. /* Cumulative data. */
  1494. unsigned long write_count = 0;
  1495. unsigned long data_count = 0;
  1496. bfd_size_type total_size = 0;
  1497. };
  1498. /* Opaque data for load_progress for a single section. */
  1499. struct load_progress_section_data
  1500. {
  1501. load_progress_section_data (load_progress_data *cumulative_,
  1502. const char *section_name_, ULONGEST section_size_,
  1503. CORE_ADDR lma_, gdb_byte *buffer_)
  1504. : cumulative (cumulative_), section_name (section_name_),
  1505. section_size (section_size_), lma (lma_), buffer (buffer_)
  1506. {}
  1507. struct load_progress_data *cumulative;
  1508. /* Per-section data. */
  1509. const char *section_name;
  1510. ULONGEST section_sent = 0;
  1511. ULONGEST section_size;
  1512. CORE_ADDR lma;
  1513. gdb_byte *buffer;
  1514. };
  1515. /* Opaque data for load_section_callback. */
  1516. struct load_section_data
  1517. {
  1518. load_section_data (load_progress_data *progress_data_)
  1519. : progress_data (progress_data_)
  1520. {}
  1521. ~load_section_data ()
  1522. {
  1523. for (auto &&request : requests)
  1524. {
  1525. xfree (request.data);
  1526. delete ((load_progress_section_data *) request.baton);
  1527. }
  1528. }
  1529. CORE_ADDR load_offset = 0;
  1530. struct load_progress_data *progress_data;
  1531. std::vector<struct memory_write_request> requests;
  1532. };
  1533. /* Target write callback routine for progress reporting. */
  1534. static void
  1535. load_progress (ULONGEST bytes, void *untyped_arg)
  1536. {
  1537. struct load_progress_section_data *args
  1538. = (struct load_progress_section_data *) untyped_arg;
  1539. struct load_progress_data *totals;
  1540. if (args == NULL)
  1541. /* Writing padding data. No easy way to get at the cumulative
  1542. stats, so just ignore this. */
  1543. return;
  1544. totals = args->cumulative;
  1545. if (bytes == 0 && args->section_sent == 0)
  1546. {
  1547. /* The write is just starting. Let the user know we've started
  1548. this section. */
  1549. current_uiout->message ("Loading section %s, size %s lma %s\n",
  1550. args->section_name,
  1551. hex_string (args->section_size),
  1552. paddress (target_gdbarch (), args->lma));
  1553. return;
  1554. }
  1555. if (validate_download)
  1556. {
  1557. /* Broken memories and broken monitors manifest themselves here
  1558. when bring new computers to life. This doubles already slow
  1559. downloads. */
  1560. /* NOTE: cagney/1999-10-18: A more efficient implementation
  1561. might add a verify_memory() method to the target vector and
  1562. then use that. remote.c could implement that method using
  1563. the ``qCRC'' packet. */
  1564. gdb::byte_vector check (bytes);
  1565. if (target_read_memory (args->lma, check.data (), bytes) != 0)
  1566. error (_("Download verify read failed at %s"),
  1567. paddress (target_gdbarch (), args->lma));
  1568. if (memcmp (args->buffer, check.data (), bytes) != 0)
  1569. error (_("Download verify compare failed at %s"),
  1570. paddress (target_gdbarch (), args->lma));
  1571. }
  1572. totals->data_count += bytes;
  1573. args->lma += bytes;
  1574. args->buffer += bytes;
  1575. totals->write_count += 1;
  1576. args->section_sent += bytes;
  1577. if (check_quit_flag ()
  1578. || (deprecated_ui_load_progress_hook != NULL
  1579. && deprecated_ui_load_progress_hook (args->section_name,
  1580. args->section_sent)))
  1581. error (_("Canceled the download"));
  1582. if (deprecated_show_load_progress != NULL)
  1583. deprecated_show_load_progress (args->section_name,
  1584. args->section_sent,
  1585. args->section_size,
  1586. totals->data_count,
  1587. totals->total_size);
  1588. }
  1589. /* Service function for generic_load. */
  1590. static void
  1591. load_one_section (bfd *abfd, asection *asec,
  1592. struct load_section_data *args)
  1593. {
  1594. bfd_size_type size = bfd_section_size (asec);
  1595. const char *sect_name = bfd_section_name (asec);
  1596. if ((bfd_section_flags (asec) & SEC_LOAD) == 0)
  1597. return;
  1598. if (size == 0)
  1599. return;
  1600. ULONGEST begin = bfd_section_lma (asec) + args->load_offset;
  1601. ULONGEST end = begin + size;
  1602. gdb_byte *buffer = (gdb_byte *) xmalloc (size);
  1603. bfd_get_section_contents (abfd, asec, buffer, 0, size);
  1604. load_progress_section_data *section_data
  1605. = new load_progress_section_data (args->progress_data, sect_name, size,
  1606. begin, buffer);
  1607. args->requests.emplace_back (begin, end, buffer, section_data);
  1608. }
  1609. static void print_transfer_performance (struct ui_file *stream,
  1610. unsigned long data_count,
  1611. unsigned long write_count,
  1612. std::chrono::steady_clock::duration d);
  1613. /* See symfile.h. */
  1614. void
  1615. generic_load (const char *args, int from_tty)
  1616. {
  1617. struct load_progress_data total_progress;
  1618. struct load_section_data cbdata (&total_progress);
  1619. struct ui_out *uiout = current_uiout;
  1620. if (args == NULL)
  1621. error_no_arg (_("file to load"));
  1622. gdb_argv argv (args);
  1623. gdb::unique_xmalloc_ptr<char> filename (tilde_expand (argv[0]));
  1624. if (argv[1] != NULL)
  1625. {
  1626. const char *endptr;
  1627. cbdata.load_offset = strtoulst (argv[1], &endptr, 0);
  1628. /* If the last word was not a valid number then
  1629. treat it as a file name with spaces in. */
  1630. if (argv[1] == endptr)
  1631. error (_("Invalid download offset:%s."), argv[1]);
  1632. if (argv[2] != NULL)
  1633. error (_("Too many parameters."));
  1634. }
  1635. /* Open the file for loading. */
  1636. gdb_bfd_ref_ptr loadfile_bfd (gdb_bfd_open (filename.get (), gnutarget));
  1637. if (loadfile_bfd == NULL)
  1638. perror_with_name (filename.get ());
  1639. if (!bfd_check_format (loadfile_bfd.get (), bfd_object))
  1640. {
  1641. error (_("\"%s\" is not an object file: %s"), filename.get (),
  1642. bfd_errmsg (bfd_get_error ()));
  1643. }
  1644. for (asection *asec : gdb_bfd_sections (loadfile_bfd))
  1645. total_progress.total_size += bfd_section_size (asec);
  1646. for (asection *asec : gdb_bfd_sections (loadfile_bfd))
  1647. load_one_section (loadfile_bfd.get (), asec, &cbdata);
  1648. using namespace std::chrono;
  1649. steady_clock::time_point start_time = steady_clock::now ();
  1650. if (target_write_memory_blocks (cbdata.requests, flash_discard,
  1651. load_progress) != 0)
  1652. error (_("Load failed"));
  1653. steady_clock::time_point end_time = steady_clock::now ();
  1654. CORE_ADDR entry = bfd_get_start_address (loadfile_bfd.get ());
  1655. entry = gdbarch_addr_bits_remove (target_gdbarch (), entry);
  1656. uiout->text ("Start address ");
  1657. uiout->field_core_addr ("address", target_gdbarch (), entry);
  1658. uiout->text (", load size ");
  1659. uiout->field_unsigned ("load-size", total_progress.data_count);
  1660. uiout->text ("\n");
  1661. regcache_write_pc (get_current_regcache (), entry);
  1662. /* Reset breakpoints, now that we have changed the load image. For
  1663. instance, breakpoints may have been set (or reset, by
  1664. post_create_inferior) while connected to the target but before we
  1665. loaded the program. In that case, the prologue analyzer could
  1666. have read instructions from the target to find the right
  1667. breakpoint locations. Loading has changed the contents of that
  1668. memory. */
  1669. breakpoint_re_set ();
  1670. print_transfer_performance (gdb_stdout, total_progress.data_count,
  1671. total_progress.write_count,
  1672. end_time - start_time);
  1673. }
  1674. /* Report on STREAM the performance of a memory transfer operation,
  1675. such as 'load'. DATA_COUNT is the number of bytes transferred.
  1676. WRITE_COUNT is the number of separate write operations, or 0, if
  1677. that information is not available. TIME is how long the operation
  1678. lasted. */
  1679. static void
  1680. print_transfer_performance (struct ui_file *stream,
  1681. unsigned long data_count,
  1682. unsigned long write_count,
  1683. std::chrono::steady_clock::duration time)
  1684. {
  1685. using namespace std::chrono;
  1686. struct ui_out *uiout = current_uiout;
  1687. milliseconds ms = duration_cast<milliseconds> (time);
  1688. uiout->text ("Transfer rate: ");
  1689. if (ms.count () > 0)
  1690. {
  1691. unsigned long rate = ((ULONGEST) data_count * 1000) / ms.count ();
  1692. if (uiout->is_mi_like_p ())
  1693. {
  1694. uiout->field_unsigned ("transfer-rate", rate * 8);
  1695. uiout->text (" bits/sec");
  1696. }
  1697. else if (rate < 1024)
  1698. {
  1699. uiout->field_unsigned ("transfer-rate", rate);
  1700. uiout->text (" bytes/sec");
  1701. }
  1702. else
  1703. {
  1704. uiout->field_unsigned ("transfer-rate", rate / 1024);
  1705. uiout->text (" KB/sec");
  1706. }
  1707. }
  1708. else
  1709. {
  1710. uiout->field_unsigned ("transferred-bits", (data_count * 8));
  1711. uiout->text (" bits in <1 sec");
  1712. }
  1713. if (write_count > 0)
  1714. {
  1715. uiout->text (", ");
  1716. uiout->field_unsigned ("write-rate", data_count / write_count);
  1717. uiout->text (" bytes/write");
  1718. }
  1719. uiout->text (".\n");
  1720. }
  1721. /* Add an OFFSET to the start address of each section in OBJF, except
  1722. sections that were specified in ADDRS. */
  1723. static void
  1724. set_objfile_default_section_offset (struct objfile *objf,
  1725. const section_addr_info &addrs,
  1726. CORE_ADDR offset)
  1727. {
  1728. /* Add OFFSET to all sections by default. */
  1729. section_offsets offsets (objf->section_offsets.size (), offset);
  1730. /* Create sorted lists of all sections in ADDRS as well as all
  1731. sections in OBJF. */
  1732. std::vector<const struct other_sections *> addrs_sorted
  1733. = addrs_section_sort (addrs);
  1734. section_addr_info objf_addrs
  1735. = build_section_addr_info_from_objfile (objf);
  1736. std::vector<const struct other_sections *> objf_addrs_sorted
  1737. = addrs_section_sort (objf_addrs);
  1738. /* Walk the BFD section list, and if a matching section is found in
  1739. ADDRS_SORTED_LIST, set its offset to zero to keep its address
  1740. unchanged.
  1741. Note that both lists may contain multiple sections with the same
  1742. name, and then the sections from ADDRS are matched in BFD order
  1743. (thanks to sectindex). */
  1744. std::vector<const struct other_sections *>::iterator addrs_sorted_iter
  1745. = addrs_sorted.begin ();
  1746. for (const other_sections *objf_sect : objf_addrs_sorted)
  1747. {
  1748. const char *objf_name = addr_section_name (objf_sect->name.c_str ());
  1749. int cmp = -1;
  1750. while (cmp < 0 && addrs_sorted_iter != addrs_sorted.end ())
  1751. {
  1752. const struct other_sections *sect = *addrs_sorted_iter;
  1753. const char *sect_name = addr_section_name (sect->name.c_str ());
  1754. cmp = strcmp (sect_name, objf_name);
  1755. if (cmp <= 0)
  1756. ++addrs_sorted_iter;
  1757. }
  1758. if (cmp == 0)
  1759. offsets[objf_sect->sectindex] = 0;
  1760. }
  1761. /* Apply the new section offsets. */
  1762. objfile_relocate (objf, offsets);
  1763. }
  1764. /* This function allows the addition of incrementally linked object files.
  1765. It does not modify any state in the target, only in the debugger. */
  1766. static void
  1767. add_symbol_file_command (const char *args, int from_tty)
  1768. {
  1769. struct gdbarch *gdbarch = get_current_arch ();
  1770. gdb::unique_xmalloc_ptr<char> filename;
  1771. char *arg;
  1772. int argcnt = 0;
  1773. struct objfile *objf;
  1774. objfile_flags flags = OBJF_USERLOADED | OBJF_SHARED;
  1775. symfile_add_flags add_flags = 0;
  1776. if (from_tty)
  1777. add_flags |= SYMFILE_VERBOSE;
  1778. struct sect_opt
  1779. {
  1780. const char *name;
  1781. const char *value;
  1782. };
  1783. std::vector<sect_opt> sect_opts = { { ".text", NULL } };
  1784. bool stop_processing_options = false;
  1785. CORE_ADDR offset = 0;
  1786. dont_repeat ();
  1787. if (args == NULL)
  1788. error (_("add-symbol-file takes a file name and an address"));
  1789. bool seen_addr = false;
  1790. bool seen_offset = false;
  1791. gdb_argv argv (args);
  1792. for (arg = argv[0], argcnt = 0; arg != NULL; arg = argv[++argcnt])
  1793. {
  1794. if (stop_processing_options || *arg != '-')
  1795. {
  1796. if (filename == NULL)
  1797. {
  1798. /* First non-option argument is always the filename. */
  1799. filename.reset (tilde_expand (arg));
  1800. }
  1801. else if (!seen_addr)
  1802. {
  1803. /* The second non-option argument is always the text
  1804. address at which to load the program. */
  1805. sect_opts[0].value = arg;
  1806. seen_addr = true;
  1807. }
  1808. else
  1809. error (_("Unrecognized argument \"%s\""), arg);
  1810. }
  1811. else if (strcmp (arg, "-readnow") == 0)
  1812. flags |= OBJF_READNOW;
  1813. else if (strcmp (arg, "-readnever") == 0)
  1814. flags |= OBJF_READNEVER;
  1815. else if (strcmp (arg, "-s") == 0)
  1816. {
  1817. if (argv[argcnt + 1] == NULL)
  1818. error (_("Missing section name after \"-s\""));
  1819. else if (argv[argcnt + 2] == NULL)
  1820. error (_("Missing section address after \"-s\""));
  1821. sect_opt sect = { argv[argcnt + 1], argv[argcnt + 2] };
  1822. sect_opts.push_back (sect);
  1823. argcnt += 2;
  1824. }
  1825. else if (strcmp (arg, "-o") == 0)
  1826. {
  1827. arg = argv[++argcnt];
  1828. if (arg == NULL)
  1829. error (_("Missing argument to -o"));
  1830. offset = parse_and_eval_address (arg);
  1831. seen_offset = true;
  1832. }
  1833. else if (strcmp (arg, "--") == 0)
  1834. stop_processing_options = true;
  1835. else
  1836. error (_("Unrecognized argument \"%s\""), arg);
  1837. }
  1838. if (filename == NULL)
  1839. error (_("You must provide a filename to be loaded."));
  1840. validate_readnow_readnever (flags);
  1841. /* Print the prompt for the query below. And save the arguments into
  1842. a sect_addr_info structure to be passed around to other
  1843. functions. We have to split this up into separate print
  1844. statements because hex_string returns a local static
  1845. string. */
  1846. gdb_printf (_("add symbol table from file \"%s\""),
  1847. filename.get ());
  1848. section_addr_info section_addrs;
  1849. std::vector<sect_opt>::const_iterator it = sect_opts.begin ();
  1850. if (!seen_addr)
  1851. ++it;
  1852. for (; it != sect_opts.end (); ++it)
  1853. {
  1854. CORE_ADDR addr;
  1855. const char *val = it->value;
  1856. const char *sec = it->name;
  1857. if (section_addrs.empty ())
  1858. gdb_printf (_(" at\n"));
  1859. addr = parse_and_eval_address (val);
  1860. /* Here we store the section offsets in the order they were
  1861. entered on the command line. Every array element is
  1862. assigned an ascending section index to preserve the above
  1863. order over an unstable sorting algorithm. This dummy
  1864. index is not used for any other purpose.
  1865. */
  1866. section_addrs.emplace_back (addr, sec, section_addrs.size ());
  1867. gdb_printf ("\t%s_addr = %s\n", sec,
  1868. paddress (gdbarch, addr));
  1869. /* The object's sections are initialized when a
  1870. call is made to build_objfile_section_table (objfile).
  1871. This happens in reread_symbols.
  1872. At this point, we don't know what file type this is,
  1873. so we can't determine what section names are valid. */
  1874. }
  1875. if (seen_offset)
  1876. gdb_printf (_("%s offset by %s\n"),
  1877. (section_addrs.empty ()
  1878. ? _(" with all sections")
  1879. : _("with other sections")),
  1880. paddress (gdbarch, offset));
  1881. else if (section_addrs.empty ())
  1882. gdb_printf ("\n");
  1883. if (from_tty && (!query ("%s", "")))
  1884. error (_("Not confirmed."));
  1885. objf = symbol_file_add (filename.get (), add_flags, &section_addrs,
  1886. flags);
  1887. if (!objfile_has_symbols (objf) && objf->per_bfd->minimal_symbol_count <= 0)
  1888. warning (_("newly-added symbol file \"%s\" does not provide any symbols"),
  1889. filename.get ());
  1890. if (seen_offset)
  1891. set_objfile_default_section_offset (objf, section_addrs, offset);
  1892. current_program_space->add_target_sections (objf);
  1893. /* Getting new symbols may change our opinion about what is
  1894. frameless. */
  1895. reinit_frame_cache ();
  1896. }
  1897. /* This function removes a symbol file that was added via add-symbol-file. */
  1898. static void
  1899. remove_symbol_file_command (const char *args, int from_tty)
  1900. {
  1901. struct objfile *objf = NULL;
  1902. struct program_space *pspace = current_program_space;
  1903. dont_repeat ();
  1904. if (args == NULL)
  1905. error (_("remove-symbol-file: no symbol file provided"));
  1906. gdb_argv argv (args);
  1907. if (strcmp (argv[0], "-a") == 0)
  1908. {
  1909. /* Interpret the next argument as an address. */
  1910. CORE_ADDR addr;
  1911. if (argv[1] == NULL)
  1912. error (_("Missing address argument"));
  1913. if (argv[2] != NULL)
  1914. error (_("Junk after %s"), argv[1]);
  1915. addr = parse_and_eval_address (argv[1]);
  1916. for (objfile *objfile : current_program_space->objfiles ())
  1917. {
  1918. if ((objfile->flags & OBJF_USERLOADED) != 0
  1919. && (objfile->flags & OBJF_SHARED) != 0
  1920. && objfile->pspace == pspace
  1921. && is_addr_in_objfile (addr, objfile))
  1922. {
  1923. objf = objfile;
  1924. break;
  1925. }
  1926. }
  1927. }
  1928. else if (argv[0] != NULL)
  1929. {
  1930. /* Interpret the current argument as a file name. */
  1931. if (argv[1] != NULL)
  1932. error (_("Junk after %s"), argv[0]);
  1933. gdb::unique_xmalloc_ptr<char> filename (tilde_expand (argv[0]));
  1934. for (objfile *objfile : current_program_space->objfiles ())
  1935. {
  1936. if ((objfile->flags & OBJF_USERLOADED) != 0
  1937. && (objfile->flags & OBJF_SHARED) != 0
  1938. && objfile->pspace == pspace
  1939. && filename_cmp (filename.get (), objfile_name (objfile)) == 0)
  1940. {
  1941. objf = objfile;
  1942. break;
  1943. }
  1944. }
  1945. }
  1946. if (objf == NULL)
  1947. error (_("No symbol file found"));
  1948. if (from_tty
  1949. && !query (_("Remove symbol table from file \"%s\"? "),
  1950. objfile_name (objf)))
  1951. error (_("Not confirmed."));
  1952. objf->unlink ();
  1953. clear_symtab_users (0);
  1954. }
  1955. /* Re-read symbols if a symbol-file has changed. */
  1956. void
  1957. reread_symbols (int from_tty)
  1958. {
  1959. long new_modtime;
  1960. struct stat new_statbuf;
  1961. int res;
  1962. std::vector<struct objfile *> new_objfiles;
  1963. for (objfile *objfile : current_program_space->objfiles ())
  1964. {
  1965. if (objfile->obfd == NULL)
  1966. continue;
  1967. /* Separate debug objfiles are handled in the main objfile. */
  1968. if (objfile->separate_debug_objfile_backlink)
  1969. continue;
  1970. /* If this object is from an archive (what you usually create with
  1971. `ar', often called a `static library' on most systems, though
  1972. a `shared library' on AIX is also an archive), then you should
  1973. stat on the archive name, not member name. */
  1974. if (objfile->obfd->my_archive)
  1975. res = stat (bfd_get_filename (objfile->obfd->my_archive), &new_statbuf);
  1976. else
  1977. res = stat (objfile_name (objfile), &new_statbuf);
  1978. if (res != 0)
  1979. {
  1980. /* FIXME, should use print_sys_errmsg but it's not filtered. */
  1981. gdb_printf (_("`%s' has disappeared; keeping its symbols.\n"),
  1982. objfile_name (objfile));
  1983. continue;
  1984. }
  1985. new_modtime = new_statbuf.st_mtime;
  1986. if (new_modtime != objfile->mtime)
  1987. {
  1988. gdb_printf (_("`%s' has changed; re-reading symbols.\n"),
  1989. objfile_name (objfile));
  1990. /* There are various functions like symbol_file_add,
  1991. symfile_bfd_open, syms_from_objfile, etc., which might
  1992. appear to do what we want. But they have various other
  1993. effects which we *don't* want. So we just do stuff
  1994. ourselves. We don't worry about mapped files (for one thing,
  1995. any mapped file will be out of date). */
  1996. /* If we get an error, blow away this objfile (not sure if
  1997. that is the correct response for things like shared
  1998. libraries). */
  1999. objfile_up objfile_holder (objfile);
  2000. /* We need to do this whenever any symbols go away. */
  2001. clear_symtab_users_cleanup defer_clear_users (0);
  2002. if (current_program_space->exec_bfd () != NULL
  2003. && filename_cmp (bfd_get_filename (objfile->obfd),
  2004. bfd_get_filename (current_program_space->exec_bfd ())) == 0)
  2005. {
  2006. /* Reload EXEC_BFD without asking anything. */
  2007. exec_file_attach (bfd_get_filename (objfile->obfd), 0);
  2008. }
  2009. /* Keep the calls order approx. the same as in free_objfile. */
  2010. /* Free the separate debug objfiles. It will be
  2011. automatically recreated by sym_read. */
  2012. free_objfile_separate_debug (objfile);
  2013. /* Clear the stale source cache. */
  2014. forget_cached_source_info ();
  2015. /* Remove any references to this objfile in the global
  2016. value lists. */
  2017. preserve_values (objfile);
  2018. /* Nuke all the state that we will re-read. Much of the following
  2019. code which sets things to NULL really is necessary to tell
  2020. other parts of GDB that there is nothing currently there.
  2021. Try to keep the freeing order compatible with free_objfile. */
  2022. if (objfile->sf != NULL)
  2023. {
  2024. (*objfile->sf->sym_finish) (objfile);
  2025. }
  2026. clear_objfile_data (objfile);
  2027. /* Clean up any state BFD has sitting around. */
  2028. {
  2029. gdb_bfd_ref_ptr obfd (objfile->obfd);
  2030. const char *obfd_filename;
  2031. obfd_filename = bfd_get_filename (objfile->obfd);
  2032. /* Open the new BFD before freeing the old one, so that
  2033. the filename remains live. */
  2034. gdb_bfd_ref_ptr temp (gdb_bfd_open (obfd_filename, gnutarget));
  2035. objfile->obfd = temp.release ();
  2036. if (objfile->obfd == NULL)
  2037. error (_("Can't open %s to read symbols."), obfd_filename);
  2038. }
  2039. std::string original_name = objfile->original_name;
  2040. /* bfd_openr sets cacheable to true, which is what we want. */
  2041. if (!bfd_check_format (objfile->obfd, bfd_object))
  2042. error (_("Can't read symbols from %s: %s."), objfile_name (objfile),
  2043. bfd_errmsg (bfd_get_error ()));
  2044. /* NB: after this call to obstack_free, objfiles_changed
  2045. will need to be called (see discussion below). */
  2046. obstack_free (&objfile->objfile_obstack, 0);
  2047. objfile->sections = NULL;
  2048. objfile->section_offsets.clear ();
  2049. objfile->sect_index_bss = -1;
  2050. objfile->sect_index_data = -1;
  2051. objfile->sect_index_rodata = -1;
  2052. objfile->sect_index_text = -1;
  2053. objfile->compunit_symtabs = NULL;
  2054. objfile->template_symbols = NULL;
  2055. objfile->static_links.reset (nullptr);
  2056. /* obstack_init also initializes the obstack so it is
  2057. empty. We could use obstack_specify_allocation but
  2058. gdb_obstack.h specifies the alloc/dealloc functions. */
  2059. obstack_init (&objfile->objfile_obstack);
  2060. /* set_objfile_per_bfd potentially allocates the per-bfd
  2061. data on the objfile's obstack (if sharing data across
  2062. multiple users is not possible), so it's important to
  2063. do it *after* the obstack has been initialized. */
  2064. set_objfile_per_bfd (objfile);
  2065. objfile->original_name
  2066. = obstack_strdup (&objfile->objfile_obstack, original_name);
  2067. /* Reset the sym_fns pointer. The ELF reader can change it
  2068. based on whether .gdb_index is present, and we need it to
  2069. start over. PR symtab/15885 */
  2070. objfile_set_sym_fns (objfile, find_sym_fns (objfile->obfd));
  2071. objfile->qf.clear ();
  2072. build_objfile_section_table (objfile);
  2073. /* What the hell is sym_new_init for, anyway? The concept of
  2074. distinguishing between the main file and additional files
  2075. in this way seems rather dubious. */
  2076. if (objfile == current_program_space->symfile_object_file)
  2077. {
  2078. (*objfile->sf->sym_new_init) (objfile);
  2079. }
  2080. (*objfile->sf->sym_init) (objfile);
  2081. clear_complaints ();
  2082. objfile->flags &= ~OBJF_PSYMTABS_READ;
  2083. /* We are about to read new symbols and potentially also
  2084. DWARF information. Some targets may want to pass addresses
  2085. read from DWARF DIE's through an adjustment function before
  2086. saving them, like MIPS, which may call into
  2087. "find_pc_section". When called, that function will make
  2088. use of per-objfile program space data.
  2089. Since we discarded our section information above, we have
  2090. dangling pointers in the per-objfile program space data
  2091. structure. Force GDB to update the section mapping
  2092. information by letting it know the objfile has changed,
  2093. making the dangling pointers point to correct data
  2094. again. */
  2095. objfiles_changed ();
  2096. /* Recompute section offsets and section indices. */
  2097. objfile->sf->sym_offsets (objfile, {});
  2098. read_symbols (objfile, 0);
  2099. if ((objfile->flags & OBJF_READNOW))
  2100. {
  2101. const int mainline = objfile->flags & OBJF_MAINLINE;
  2102. const int should_print = (print_symbol_loading_p (from_tty, mainline, 1)
  2103. && readnow_symbol_files);
  2104. if (should_print)
  2105. gdb_printf (_("Expanding full symbols from %ps...\n"),
  2106. styled_string (file_name_style.style (),
  2107. objfile_name (objfile)));
  2108. objfile->expand_all_symtabs ();
  2109. }
  2110. if (!objfile_has_symbols (objfile))
  2111. {
  2112. gdb_stdout->wrap_here (0);
  2113. gdb_printf (_("(no debugging symbols found)\n"));
  2114. gdb_stdout->wrap_here (0);
  2115. }
  2116. /* We're done reading the symbol file; finish off complaints. */
  2117. clear_complaints ();
  2118. /* Getting new symbols may change our opinion about what is
  2119. frameless. */
  2120. reinit_frame_cache ();
  2121. /* Discard cleanups as symbol reading was successful. */
  2122. objfile_holder.release ();
  2123. defer_clear_users.release ();
  2124. /* If the mtime has changed between the time we set new_modtime
  2125. and now, we *want* this to be out of date, so don't call stat
  2126. again now. */
  2127. objfile->mtime = new_modtime;
  2128. init_entry_point_info (objfile);
  2129. new_objfiles.push_back (objfile);
  2130. }
  2131. }
  2132. if (!new_objfiles.empty ())
  2133. {
  2134. clear_symtab_users (0);
  2135. /* clear_objfile_data for each objfile was called before freeing it and
  2136. gdb::observers::new_objfile.notify (NULL) has been called by
  2137. clear_symtab_users above. Notify the new files now. */
  2138. for (auto iter : new_objfiles)
  2139. gdb::observers::new_objfile.notify (iter);
  2140. /* At least one objfile has changed, so we can consider that
  2141. the executable we're debugging has changed too. */
  2142. gdb::observers::executable_changed.notify ();
  2143. }
  2144. }
  2145. struct filename_language
  2146. {
  2147. filename_language (const std::string &ext_, enum language lang_)
  2148. : ext (ext_), lang (lang_)
  2149. {}
  2150. std::string ext;
  2151. enum language lang;
  2152. };
  2153. static std::vector<filename_language> filename_language_table;
  2154. /* See symfile.h. */
  2155. void
  2156. add_filename_language (const char *ext, enum language lang)
  2157. {
  2158. gdb_assert (ext != nullptr);
  2159. filename_language_table.emplace_back (ext, lang);
  2160. }
  2161. static std::string ext_args;
  2162. static void
  2163. show_ext_args (struct ui_file *file, int from_tty,
  2164. struct cmd_list_element *c, const char *value)
  2165. {
  2166. gdb_printf (file,
  2167. _("Mapping between filename extension "
  2168. "and source language is \"%s\".\n"),
  2169. value);
  2170. }
  2171. static void
  2172. set_ext_lang_command (const char *args,
  2173. int from_tty, struct cmd_list_element *e)
  2174. {
  2175. const char *begin = ext_args.c_str ();
  2176. const char *end = ext_args.c_str ();
  2177. /* First arg is filename extension, starting with '.' */
  2178. if (*end != '.')
  2179. error (_("'%s': Filename extension must begin with '.'"), ext_args.c_str ());
  2180. /* Find end of first arg. */
  2181. while (*end != '\0' && !isspace (*end))
  2182. end++;
  2183. if (*end == '\0')
  2184. error (_("'%s': two arguments required -- "
  2185. "filename extension and language"),
  2186. ext_args.c_str ());
  2187. /* Extract first arg, the extension. */
  2188. std::string extension = ext_args.substr (0, end - begin);
  2189. /* Find beginning of second arg, which should be a source language. */
  2190. begin = skip_spaces (end);
  2191. if (*begin == '\0')
  2192. error (_("'%s': two arguments required -- "
  2193. "filename extension and language"),
  2194. ext_args.c_str ());
  2195. /* Lookup the language from among those we know. */
  2196. language lang = language_enum (begin);
  2197. auto it = filename_language_table.begin ();
  2198. /* Now lookup the filename extension: do we already know it? */
  2199. for (; it != filename_language_table.end (); it++)
  2200. {
  2201. if (it->ext == extension)
  2202. break;
  2203. }
  2204. if (it == filename_language_table.end ())
  2205. {
  2206. /* New file extension. */
  2207. add_filename_language (extension.data (), lang);
  2208. }
  2209. else
  2210. {
  2211. /* Redefining a previously known filename extension. */
  2212. /* if (from_tty) */
  2213. /* query ("Really make files of type %s '%s'?", */
  2214. /* ext_args, language_str (lang)); */
  2215. it->lang = lang;
  2216. }
  2217. }
  2218. static void
  2219. info_ext_lang_command (const char *args, int from_tty)
  2220. {
  2221. gdb_printf (_("Filename extensions and the languages they represent:"));
  2222. gdb_printf ("\n\n");
  2223. for (const filename_language &entry : filename_language_table)
  2224. gdb_printf ("\t%s\t- %s\n", entry.ext.c_str (),
  2225. language_str (entry.lang));
  2226. }
  2227. enum language
  2228. deduce_language_from_filename (const char *filename)
  2229. {
  2230. const char *cp;
  2231. if (filename != NULL)
  2232. if ((cp = strrchr (filename, '.')) != NULL)
  2233. {
  2234. for (const filename_language &entry : filename_language_table)
  2235. if (entry.ext == cp)
  2236. return entry.lang;
  2237. }
  2238. return language_unknown;
  2239. }
  2240. /* Allocate and initialize a new symbol table.
  2241. CUST is from the result of allocate_compunit_symtab. */
  2242. struct symtab *
  2243. allocate_symtab (struct compunit_symtab *cust, const char *filename)
  2244. {
  2245. struct objfile *objfile = cust->objfile ();
  2246. struct symtab *symtab
  2247. = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symtab);
  2248. symtab->filename = objfile->intern (filename);
  2249. symtab->fullname = NULL;
  2250. symtab->set_language (deduce_language_from_filename (filename));
  2251. /* This can be very verbose with lots of headers.
  2252. Only print at higher debug levels. */
  2253. if (symtab_create_debug >= 2)
  2254. {
  2255. /* Be a bit clever with debugging messages, and don't print objfile
  2256. every time, only when it changes. */
  2257. static std::string last_objfile_name;
  2258. const char *this_objfile_name = objfile_name (objfile);
  2259. if (last_objfile_name.empty () || last_objfile_name != this_objfile_name)
  2260. {
  2261. last_objfile_name = this_objfile_name;
  2262. gdb_printf (gdb_stdlog,
  2263. "Creating one or more symtabs for objfile %s ...\n",
  2264. this_objfile_name);
  2265. }
  2266. gdb_printf (gdb_stdlog,
  2267. "Created symtab %s for module %s.\n",
  2268. host_address_to_string (symtab), filename);
  2269. }
  2270. /* Add it to CUST's list of symtabs. */
  2271. cust->add_filetab (symtab);
  2272. /* Backlink to the containing compunit symtab. */
  2273. symtab->set_compunit (cust);
  2274. return symtab;
  2275. }
  2276. /* Allocate and initialize a new compunit.
  2277. NAME is the name of the main source file, if there is one, or some
  2278. descriptive text if there are no source files. */
  2279. struct compunit_symtab *
  2280. allocate_compunit_symtab (struct objfile *objfile, const char *name)
  2281. {
  2282. struct compunit_symtab *cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
  2283. struct compunit_symtab);
  2284. const char *saved_name;
  2285. cu->set_objfile (objfile);
  2286. /* The name we record here is only for display/debugging purposes.
  2287. Just save the basename to avoid path issues (too long for display,
  2288. relative vs absolute, etc.). */
  2289. saved_name = lbasename (name);
  2290. cu->name = obstack_strdup (&objfile->objfile_obstack, saved_name);
  2291. cu->set_debugformat ("unknown");
  2292. if (symtab_create_debug)
  2293. {
  2294. gdb_printf (gdb_stdlog,
  2295. "Created compunit symtab %s for %s.\n",
  2296. host_address_to_string (cu),
  2297. cu->name);
  2298. }
  2299. return cu;
  2300. }
  2301. /* Hook CU to the objfile it comes from. */
  2302. void
  2303. add_compunit_symtab_to_objfile (struct compunit_symtab *cu)
  2304. {
  2305. cu->next = cu->objfile ()->compunit_symtabs;
  2306. cu->objfile ()->compunit_symtabs = cu;
  2307. }
  2308. /* Reset all data structures in gdb which may contain references to
  2309. symbol table data. */
  2310. void
  2311. clear_symtab_users (symfile_add_flags add_flags)
  2312. {
  2313. /* Someday, we should do better than this, by only blowing away
  2314. the things that really need to be blown. */
  2315. /* Clear the "current" symtab first, because it is no longer valid.
  2316. breakpoint_re_set may try to access the current symtab. */
  2317. clear_current_source_symtab_and_line ();
  2318. clear_displays ();
  2319. clear_last_displayed_sal ();
  2320. clear_pc_function_cache ();
  2321. gdb::observers::new_objfile.notify (NULL);
  2322. /* Varobj may refer to old symbols, perform a cleanup. */
  2323. varobj_invalidate ();
  2324. /* Now that the various caches have been cleared, we can re_set
  2325. our breakpoints without risking it using stale data. */
  2326. if ((add_flags & SYMFILE_DEFER_BP_RESET) == 0)
  2327. breakpoint_re_set ();
  2328. }
  2329. /* OVERLAYS:
  2330. The following code implements an abstraction for debugging overlay sections.
  2331. The target model is as follows:
  2332. 1) The gnu linker will permit multiple sections to be mapped into the
  2333. same VMA, each with its own unique LMA (or load address).
  2334. 2) It is assumed that some runtime mechanism exists for mapping the
  2335. sections, one by one, from the load address into the VMA address.
  2336. 3) This code provides a mechanism for gdb to keep track of which
  2337. sections should be considered to be mapped from the VMA to the LMA.
  2338. This information is used for symbol lookup, and memory read/write.
  2339. For instance, if a section has been mapped then its contents
  2340. should be read from the VMA, otherwise from the LMA.
  2341. Two levels of debugger support for overlays are available. One is
  2342. "manual", in which the debugger relies on the user to tell it which
  2343. overlays are currently mapped. This level of support is
  2344. implemented entirely in the core debugger, and the information about
  2345. whether a section is mapped is kept in the objfile->obj_section table.
  2346. The second level of support is "automatic", and is only available if
  2347. the target-specific code provides functionality to read the target's
  2348. overlay mapping table, and translate its contents for the debugger
  2349. (by updating the mapped state information in the obj_section tables).
  2350. The interface is as follows:
  2351. User commands:
  2352. overlay map <name> -- tell gdb to consider this section mapped
  2353. overlay unmap <name> -- tell gdb to consider this section unmapped
  2354. overlay list -- list the sections that GDB thinks are mapped
  2355. overlay read-target -- get the target's state of what's mapped
  2356. overlay off/manual/auto -- set overlay debugging state
  2357. Functional interface:
  2358. find_pc_mapped_section(pc): if the pc is in the range of a mapped
  2359. section, return that section.
  2360. find_pc_overlay(pc): find any overlay section that contains
  2361. the pc, either in its VMA or its LMA
  2362. section_is_mapped(sect): true if overlay is marked as mapped
  2363. section_is_overlay(sect): true if section's VMA != LMA
  2364. pc_in_mapped_range(pc,sec): true if pc belongs to section's VMA
  2365. pc_in_unmapped_range(...): true if pc belongs to section's LMA
  2366. sections_overlap(sec1, sec2): true if mapped sec1 and sec2 ranges overlap
  2367. overlay_mapped_address(...): map an address from section's LMA to VMA
  2368. overlay_unmapped_address(...): map an address from section's VMA to LMA
  2369. symbol_overlayed_address(...): Return a "current" address for symbol:
  2370. either in VMA or LMA depending on whether
  2371. the symbol's section is currently mapped. */
  2372. /* Overlay debugging state: */
  2373. enum overlay_debugging_state overlay_debugging = ovly_off;
  2374. int overlay_cache_invalid = 0; /* True if need to refresh mapped state. */
  2375. /* Function: section_is_overlay (SECTION)
  2376. Returns true if SECTION has VMA not equal to LMA, ie.
  2377. SECTION is loaded at an address different from where it will "run". */
  2378. int
  2379. section_is_overlay (struct obj_section *section)
  2380. {
  2381. if (overlay_debugging && section)
  2382. {
  2383. asection *bfd_section = section->the_bfd_section;
  2384. if (bfd_section_lma (bfd_section) != 0
  2385. && bfd_section_lma (bfd_section) != bfd_section_vma (bfd_section))
  2386. return 1;
  2387. }
  2388. return 0;
  2389. }
  2390. /* Function: overlay_invalidate_all (void)
  2391. Invalidate the mapped state of all overlay sections (mark it as stale). */
  2392. static void
  2393. overlay_invalidate_all (void)
  2394. {
  2395. struct obj_section *sect;
  2396. for (objfile *objfile : current_program_space->objfiles ())
  2397. ALL_OBJFILE_OSECTIONS (objfile, sect)
  2398. if (section_is_overlay (sect))
  2399. sect->ovly_mapped = -1;
  2400. }
  2401. /* Function: section_is_mapped (SECTION)
  2402. Returns true if section is an overlay, and is currently mapped.
  2403. Access to the ovly_mapped flag is restricted to this function, so
  2404. that we can do automatic update. If the global flag
  2405. OVERLAY_CACHE_INVALID is set (by wait_for_inferior), then call
  2406. overlay_invalidate_all. If the mapped state of the particular
  2407. section is stale, then call TARGET_OVERLAY_UPDATE to refresh it. */
  2408. int
  2409. section_is_mapped (struct obj_section *osect)
  2410. {
  2411. struct gdbarch *gdbarch;
  2412. if (osect == 0 || !section_is_overlay (osect))
  2413. return 0;
  2414. switch (overlay_debugging)
  2415. {
  2416. default:
  2417. case ovly_off:
  2418. return 0; /* overlay debugging off */
  2419. case ovly_auto: /* overlay debugging automatic */
  2420. /* Unles there is a gdbarch_overlay_update function,
  2421. there's really nothing useful to do here (can't really go auto). */
  2422. gdbarch = osect->objfile->arch ();
  2423. if (gdbarch_overlay_update_p (gdbarch))
  2424. {
  2425. if (overlay_cache_invalid)
  2426. {
  2427. overlay_invalidate_all ();
  2428. overlay_cache_invalid = 0;
  2429. }
  2430. if (osect->ovly_mapped == -1)
  2431. gdbarch_overlay_update (gdbarch, osect);
  2432. }
  2433. /* fall thru */
  2434. case ovly_on: /* overlay debugging manual */
  2435. return osect->ovly_mapped == 1;
  2436. }
  2437. }
  2438. /* Function: pc_in_unmapped_range
  2439. If PC falls into the lma range of SECTION, return true, else false. */
  2440. CORE_ADDR
  2441. pc_in_unmapped_range (CORE_ADDR pc, struct obj_section *section)
  2442. {
  2443. if (section_is_overlay (section))
  2444. {
  2445. asection *bfd_section = section->the_bfd_section;
  2446. /* We assume the LMA is relocated by the same offset as the VMA. */
  2447. bfd_vma size = bfd_section_size (bfd_section);
  2448. CORE_ADDR offset = section->offset ();
  2449. if (bfd_section_lma (bfd_section) + offset <= pc
  2450. && pc < bfd_section_lma (bfd_section) + offset + size)
  2451. return 1;
  2452. }
  2453. return 0;
  2454. }
  2455. /* Function: pc_in_mapped_range
  2456. If PC falls into the vma range of SECTION, return true, else false. */
  2457. CORE_ADDR
  2458. pc_in_mapped_range (CORE_ADDR pc, struct obj_section *section)
  2459. {
  2460. if (section_is_overlay (section))
  2461. {
  2462. if (section->addr () <= pc
  2463. && pc < section->endaddr ())
  2464. return 1;
  2465. }
  2466. return 0;
  2467. }
  2468. /* Return true if the mapped ranges of sections A and B overlap, false
  2469. otherwise. */
  2470. static int
  2471. sections_overlap (struct obj_section *a, struct obj_section *b)
  2472. {
  2473. CORE_ADDR a_start = a->addr ();
  2474. CORE_ADDR a_end = a->endaddr ();
  2475. CORE_ADDR b_start = b->addr ();
  2476. CORE_ADDR b_end = b->endaddr ();
  2477. return (a_start < b_end && b_start < a_end);
  2478. }
  2479. /* Function: overlay_unmapped_address (PC, SECTION)
  2480. Returns the address corresponding to PC in the unmapped (load) range.
  2481. May be the same as PC. */
  2482. CORE_ADDR
  2483. overlay_unmapped_address (CORE_ADDR pc, struct obj_section *section)
  2484. {
  2485. if (section_is_overlay (section) && pc_in_mapped_range (pc, section))
  2486. {
  2487. asection *bfd_section = section->the_bfd_section;
  2488. return (pc + bfd_section_lma (bfd_section)
  2489. - bfd_section_vma (bfd_section));
  2490. }
  2491. return pc;
  2492. }
  2493. /* Function: overlay_mapped_address (PC, SECTION)
  2494. Returns the address corresponding to PC in the mapped (runtime) range.
  2495. May be the same as PC. */
  2496. CORE_ADDR
  2497. overlay_mapped_address (CORE_ADDR pc, struct obj_section *section)
  2498. {
  2499. if (section_is_overlay (section) && pc_in_unmapped_range (pc, section))
  2500. {
  2501. asection *bfd_section = section->the_bfd_section;
  2502. return (pc + bfd_section_vma (bfd_section)
  2503. - bfd_section_lma (bfd_section));
  2504. }
  2505. return pc;
  2506. }
  2507. /* Function: symbol_overlayed_address
  2508. Return one of two addresses (relative to the VMA or to the LMA),
  2509. depending on whether the section is mapped or not. */
  2510. CORE_ADDR
  2511. symbol_overlayed_address (CORE_ADDR address, struct obj_section *section)
  2512. {
  2513. if (overlay_debugging)
  2514. {
  2515. /* If the symbol has no section, just return its regular address. */
  2516. if (section == 0)
  2517. return address;
  2518. /* If the symbol's section is not an overlay, just return its
  2519. address. */
  2520. if (!section_is_overlay (section))
  2521. return address;
  2522. /* If the symbol's section is mapped, just return its address. */
  2523. if (section_is_mapped (section))
  2524. return address;
  2525. /*
  2526. * HOWEVER: if the symbol is in an overlay section which is NOT mapped,
  2527. * then return its LOADED address rather than its vma address!!
  2528. */
  2529. return overlay_unmapped_address (address, section);
  2530. }
  2531. return address;
  2532. }
  2533. /* Function: find_pc_overlay (PC)
  2534. Return the best-match overlay section for PC:
  2535. If PC matches a mapped overlay section's VMA, return that section.
  2536. Else if PC matches an unmapped section's VMA, return that section.
  2537. Else if PC matches an unmapped section's LMA, return that section. */
  2538. struct obj_section *
  2539. find_pc_overlay (CORE_ADDR pc)
  2540. {
  2541. struct obj_section *osect, *best_match = NULL;
  2542. if (overlay_debugging)
  2543. {
  2544. for (objfile *objfile : current_program_space->objfiles ())
  2545. ALL_OBJFILE_OSECTIONS (objfile, osect)
  2546. if (section_is_overlay (osect))
  2547. {
  2548. if (pc_in_mapped_range (pc, osect))
  2549. {
  2550. if (section_is_mapped (osect))
  2551. return osect;
  2552. else
  2553. best_match = osect;
  2554. }
  2555. else if (pc_in_unmapped_range (pc, osect))
  2556. best_match = osect;
  2557. }
  2558. }
  2559. return best_match;
  2560. }
  2561. /* Function: find_pc_mapped_section (PC)
  2562. If PC falls into the VMA address range of an overlay section that is
  2563. currently marked as MAPPED, return that section. Else return NULL. */
  2564. struct obj_section *
  2565. find_pc_mapped_section (CORE_ADDR pc)
  2566. {
  2567. struct obj_section *osect;
  2568. if (overlay_debugging)
  2569. {
  2570. for (objfile *objfile : current_program_space->objfiles ())
  2571. ALL_OBJFILE_OSECTIONS (objfile, osect)
  2572. if (pc_in_mapped_range (pc, osect) && section_is_mapped (osect))
  2573. return osect;
  2574. }
  2575. return NULL;
  2576. }
  2577. /* Function: list_overlays_command
  2578. Print a list of mapped sections and their PC ranges. */
  2579. static void
  2580. list_overlays_command (const char *args, int from_tty)
  2581. {
  2582. int nmapped = 0;
  2583. struct obj_section *osect;
  2584. if (overlay_debugging)
  2585. {
  2586. for (objfile *objfile : current_program_space->objfiles ())
  2587. ALL_OBJFILE_OSECTIONS (objfile, osect)
  2588. if (section_is_mapped (osect))
  2589. {
  2590. struct gdbarch *gdbarch = objfile->arch ();
  2591. const char *name;
  2592. bfd_vma lma, vma;
  2593. int size;
  2594. vma = bfd_section_vma (osect->the_bfd_section);
  2595. lma = bfd_section_lma (osect->the_bfd_section);
  2596. size = bfd_section_size (osect->the_bfd_section);
  2597. name = bfd_section_name (osect->the_bfd_section);
  2598. gdb_printf ("Section %s, loaded at ", name);
  2599. gdb_puts (paddress (gdbarch, lma));
  2600. gdb_puts (" - ");
  2601. gdb_puts (paddress (gdbarch, lma + size));
  2602. gdb_printf (", mapped at ");
  2603. gdb_puts (paddress (gdbarch, vma));
  2604. gdb_puts (" - ");
  2605. gdb_puts (paddress (gdbarch, vma + size));
  2606. gdb_puts ("\n");
  2607. nmapped++;
  2608. }
  2609. }
  2610. if (nmapped == 0)
  2611. gdb_printf (_("No sections are mapped.\n"));
  2612. }
  2613. /* Function: map_overlay_command
  2614. Mark the named section as mapped (ie. residing at its VMA address). */
  2615. static void
  2616. map_overlay_command (const char *args, int from_tty)
  2617. {
  2618. struct obj_section *sec, *sec2;
  2619. if (!overlay_debugging)
  2620. error (_("Overlay debugging not enabled. Use "
  2621. "either the 'overlay auto' or\n"
  2622. "the 'overlay manual' command."));
  2623. if (args == 0 || *args == 0)
  2624. error (_("Argument required: name of an overlay section"));
  2625. /* First, find a section matching the user supplied argument. */
  2626. for (objfile *obj_file : current_program_space->objfiles ())
  2627. ALL_OBJFILE_OSECTIONS (obj_file, sec)
  2628. if (!strcmp (bfd_section_name (sec->the_bfd_section), args))
  2629. {
  2630. /* Now, check to see if the section is an overlay. */
  2631. if (!section_is_overlay (sec))
  2632. continue; /* not an overlay section */
  2633. /* Mark the overlay as "mapped". */
  2634. sec->ovly_mapped = 1;
  2635. /* Next, make a pass and unmap any sections that are
  2636. overlapped by this new section: */
  2637. for (objfile *objfile2 : current_program_space->objfiles ())
  2638. ALL_OBJFILE_OSECTIONS (objfile2, sec2)
  2639. if (sec2->ovly_mapped && sec != sec2 && sections_overlap (sec,
  2640. sec2))
  2641. {
  2642. if (info_verbose)
  2643. gdb_printf (_("Note: section %s unmapped by overlap\n"),
  2644. bfd_section_name (sec2->the_bfd_section));
  2645. sec2->ovly_mapped = 0; /* sec2 overlaps sec: unmap sec2. */
  2646. }
  2647. return;
  2648. }
  2649. error (_("No overlay section called %s"), args);
  2650. }
  2651. /* Function: unmap_overlay_command
  2652. Mark the overlay section as unmapped
  2653. (ie. resident in its LMA address range, rather than the VMA range). */
  2654. static void
  2655. unmap_overlay_command (const char *args, int from_tty)
  2656. {
  2657. struct obj_section *sec = NULL;
  2658. if (!overlay_debugging)
  2659. error (_("Overlay debugging not enabled. "
  2660. "Use either the 'overlay auto' or\n"
  2661. "the 'overlay manual' command."));
  2662. if (args == 0 || *args == 0)
  2663. error (_("Argument required: name of an overlay section"));
  2664. /* First, find a section matching the user supplied argument. */
  2665. for (objfile *objfile : current_program_space->objfiles ())
  2666. ALL_OBJFILE_OSECTIONS (objfile, sec)
  2667. if (!strcmp (bfd_section_name (sec->the_bfd_section), args))
  2668. {
  2669. if (!sec->ovly_mapped)
  2670. error (_("Section %s is not mapped"), args);
  2671. sec->ovly_mapped = 0;
  2672. return;
  2673. }
  2674. error (_("No overlay section called %s"), args);
  2675. }
  2676. /* Function: overlay_auto_command
  2677. A utility command to turn on overlay debugging.
  2678. Possibly this should be done via a set/show command. */
  2679. static void
  2680. overlay_auto_command (const char *args, int from_tty)
  2681. {
  2682. overlay_debugging = ovly_auto;
  2683. enable_overlay_breakpoints ();
  2684. if (info_verbose)
  2685. gdb_printf (_("Automatic overlay debugging enabled."));
  2686. }
  2687. /* Function: overlay_manual_command
  2688. A utility command to turn on overlay debugging.
  2689. Possibly this should be done via a set/show command. */
  2690. static void
  2691. overlay_manual_command (const char *args, int from_tty)
  2692. {
  2693. overlay_debugging = ovly_on;
  2694. disable_overlay_breakpoints ();
  2695. if (info_verbose)
  2696. gdb_printf (_("Overlay debugging enabled."));
  2697. }
  2698. /* Function: overlay_off_command
  2699. A utility command to turn on overlay debugging.
  2700. Possibly this should be done via a set/show command. */
  2701. static void
  2702. overlay_off_command (const char *args, int from_tty)
  2703. {
  2704. overlay_debugging = ovly_off;
  2705. disable_overlay_breakpoints ();
  2706. if (info_verbose)
  2707. gdb_printf (_("Overlay debugging disabled."));
  2708. }
  2709. static void
  2710. overlay_load_command (const char *args, int from_tty)
  2711. {
  2712. struct gdbarch *gdbarch = get_current_arch ();
  2713. if (gdbarch_overlay_update_p (gdbarch))
  2714. gdbarch_overlay_update (gdbarch, NULL);
  2715. else
  2716. error (_("This target does not know how to read its overlay state."));
  2717. }
  2718. /* Command list chain containing all defined "overlay" subcommands. */
  2719. static struct cmd_list_element *overlaylist;
  2720. /* Target Overlays for the "Simplest" overlay manager:
  2721. This is GDB's default target overlay layer. It works with the
  2722. minimal overlay manager supplied as an example by Cygnus. The
  2723. entry point is via a function pointer "gdbarch_overlay_update",
  2724. so targets that use a different runtime overlay manager can
  2725. substitute their own overlay_update function and take over the
  2726. function pointer.
  2727. The overlay_update function pokes around in the target's data structures
  2728. to see what overlays are mapped, and updates GDB's overlay mapping with
  2729. this information.
  2730. In this simple implementation, the target data structures are as follows:
  2731. unsigned _novlys; /# number of overlay sections #/
  2732. unsigned _ovly_table[_novlys][4] = {
  2733. {VMA, OSIZE, LMA, MAPPED}, /# one entry per overlay section #/
  2734. {..., ..., ..., ...},
  2735. }
  2736. unsigned _novly_regions; /# number of overlay regions #/
  2737. unsigned _ovly_region_table[_novly_regions][3] = {
  2738. {VMA, OSIZE, MAPPED_TO_LMA}, /# one entry per overlay region #/
  2739. {..., ..., ...},
  2740. }
  2741. These functions will attempt to update GDB's mappedness state in the
  2742. symbol section table, based on the target's mappedness state.
  2743. To do this, we keep a cached copy of the target's _ovly_table, and
  2744. attempt to detect when the cached copy is invalidated. The main
  2745. entry point is "simple_overlay_update(SECT), which looks up SECT in
  2746. the cached table and re-reads only the entry for that section from
  2747. the target (whenever possible). */
  2748. /* Cached, dynamically allocated copies of the target data structures: */
  2749. static unsigned (*cache_ovly_table)[4] = 0;
  2750. static unsigned cache_novlys = 0;
  2751. static CORE_ADDR cache_ovly_table_base = 0;
  2752. enum ovly_index
  2753. {
  2754. VMA, OSIZE, LMA, MAPPED
  2755. };
  2756. /* Throw away the cached copy of _ovly_table. */
  2757. static void
  2758. simple_free_overlay_table (void)
  2759. {
  2760. xfree (cache_ovly_table);
  2761. cache_novlys = 0;
  2762. cache_ovly_table = NULL;
  2763. cache_ovly_table_base = 0;
  2764. }
  2765. /* Read an array of ints of size SIZE from the target into a local buffer.
  2766. Convert to host order. int LEN is number of ints. */
  2767. static void
  2768. read_target_long_array (CORE_ADDR memaddr, unsigned int *myaddr,
  2769. int len, int size, enum bfd_endian byte_order)
  2770. {
  2771. /* FIXME (alloca): Not safe if array is very large. */
  2772. gdb_byte *buf = (gdb_byte *) alloca (len * size);
  2773. int i;
  2774. read_memory (memaddr, buf, len * size);
  2775. for (i = 0; i < len; i++)
  2776. myaddr[i] = extract_unsigned_integer (size * i + buf, size, byte_order);
  2777. }
  2778. /* Find and grab a copy of the target _ovly_table
  2779. (and _novlys, which is needed for the table's size). */
  2780. static int
  2781. simple_read_overlay_table (void)
  2782. {
  2783. struct bound_minimal_symbol novlys_msym;
  2784. struct bound_minimal_symbol ovly_table_msym;
  2785. struct gdbarch *gdbarch;
  2786. int word_size;
  2787. enum bfd_endian byte_order;
  2788. simple_free_overlay_table ();
  2789. novlys_msym = lookup_minimal_symbol ("_novlys", NULL, NULL);
  2790. if (! novlys_msym.minsym)
  2791. {
  2792. error (_("Error reading inferior's overlay table: "
  2793. "couldn't find `_novlys' variable\n"
  2794. "in inferior. Use `overlay manual' mode."));
  2795. return 0;
  2796. }
  2797. ovly_table_msym = lookup_bound_minimal_symbol ("_ovly_table");
  2798. if (! ovly_table_msym.minsym)
  2799. {
  2800. error (_("Error reading inferior's overlay table: couldn't find "
  2801. "`_ovly_table' array\n"
  2802. "in inferior. Use `overlay manual' mode."));
  2803. return 0;
  2804. }
  2805. gdbarch = ovly_table_msym.objfile->arch ();
  2806. word_size = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
  2807. byte_order = gdbarch_byte_order (gdbarch);
  2808. cache_novlys = read_memory_integer (BMSYMBOL_VALUE_ADDRESS (novlys_msym),
  2809. 4, byte_order);
  2810. cache_ovly_table
  2811. = (unsigned int (*)[4]) xmalloc (cache_novlys * sizeof (*cache_ovly_table));
  2812. cache_ovly_table_base = BMSYMBOL_VALUE_ADDRESS (ovly_table_msym);
  2813. read_target_long_array (cache_ovly_table_base,
  2814. (unsigned int *) cache_ovly_table,
  2815. cache_novlys * 4, word_size, byte_order);
  2816. return 1; /* SUCCESS */
  2817. }
  2818. /* Function: simple_overlay_update_1
  2819. A helper function for simple_overlay_update. Assuming a cached copy
  2820. of _ovly_table exists, look through it to find an entry whose vma,
  2821. lma and size match those of OSECT. Re-read the entry and make sure
  2822. it still matches OSECT (else the table may no longer be valid).
  2823. Set OSECT's mapped state to match the entry. Return: 1 for
  2824. success, 0 for failure. */
  2825. static int
  2826. simple_overlay_update_1 (struct obj_section *osect)
  2827. {
  2828. int i;
  2829. asection *bsect = osect->the_bfd_section;
  2830. struct gdbarch *gdbarch = osect->objfile->arch ();
  2831. int word_size = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
  2832. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2833. for (i = 0; i < cache_novlys; i++)
  2834. if (cache_ovly_table[i][VMA] == bfd_section_vma (bsect)
  2835. && cache_ovly_table[i][LMA] == bfd_section_lma (bsect))
  2836. {
  2837. read_target_long_array (cache_ovly_table_base + i * word_size,
  2838. (unsigned int *) cache_ovly_table[i],
  2839. 4, word_size, byte_order);
  2840. if (cache_ovly_table[i][VMA] == bfd_section_vma (bsect)
  2841. && cache_ovly_table[i][LMA] == bfd_section_lma (bsect))
  2842. {
  2843. osect->ovly_mapped = cache_ovly_table[i][MAPPED];
  2844. return 1;
  2845. }
  2846. else /* Warning! Warning! Target's ovly table has changed! */
  2847. return 0;
  2848. }
  2849. return 0;
  2850. }
  2851. /* Function: simple_overlay_update
  2852. If OSECT is NULL, then update all sections' mapped state
  2853. (after re-reading the entire target _ovly_table).
  2854. If OSECT is non-NULL, then try to find a matching entry in the
  2855. cached ovly_table and update only OSECT's mapped state.
  2856. If a cached entry can't be found or the cache isn't valid, then
  2857. re-read the entire cache, and go ahead and update all sections. */
  2858. void
  2859. simple_overlay_update (struct obj_section *osect)
  2860. {
  2861. /* Were we given an osect to look up? NULL means do all of them. */
  2862. if (osect)
  2863. /* Have we got a cached copy of the target's overlay table? */
  2864. if (cache_ovly_table != NULL)
  2865. {
  2866. /* Does its cached location match what's currently in the
  2867. symtab? */
  2868. struct bound_minimal_symbol minsym
  2869. = lookup_minimal_symbol ("_ovly_table", NULL, NULL);
  2870. if (minsym.minsym == NULL)
  2871. error (_("Error reading inferior's overlay table: couldn't "
  2872. "find `_ovly_table' array\n"
  2873. "in inferior. Use `overlay manual' mode."));
  2874. if (cache_ovly_table_base == BMSYMBOL_VALUE_ADDRESS (minsym))
  2875. /* Then go ahead and try to look up this single section in
  2876. the cache. */
  2877. if (simple_overlay_update_1 (osect))
  2878. /* Found it! We're done. */
  2879. return;
  2880. }
  2881. /* Cached table no good: need to read the entire table anew.
  2882. Or else we want all the sections, in which case it's actually
  2883. more efficient to read the whole table in one block anyway. */
  2884. if (! simple_read_overlay_table ())
  2885. return;
  2886. /* Now may as well update all sections, even if only one was requested. */
  2887. for (objfile *objfile : current_program_space->objfiles ())
  2888. ALL_OBJFILE_OSECTIONS (objfile, osect)
  2889. if (section_is_overlay (osect))
  2890. {
  2891. int i;
  2892. asection *bsect = osect->the_bfd_section;
  2893. for (i = 0; i < cache_novlys; i++)
  2894. if (cache_ovly_table[i][VMA] == bfd_section_vma (bsect)
  2895. && cache_ovly_table[i][LMA] == bfd_section_lma (bsect))
  2896. { /* obj_section matches i'th entry in ovly_table. */
  2897. osect->ovly_mapped = cache_ovly_table[i][MAPPED];
  2898. break; /* finished with inner for loop: break out. */
  2899. }
  2900. }
  2901. }
  2902. /* Default implementation for sym_relocate. */
  2903. bfd_byte *
  2904. default_symfile_relocate (struct objfile *objfile, asection *sectp,
  2905. bfd_byte *buf)
  2906. {
  2907. /* Use sectp->owner instead of objfile->obfd. sectp may point to a
  2908. DWO file. */
  2909. bfd *abfd = sectp->owner;
  2910. /* We're only interested in sections with relocation
  2911. information. */
  2912. if ((sectp->flags & SEC_RELOC) == 0)
  2913. return NULL;
  2914. /* We will handle section offsets properly elsewhere, so relocate as if
  2915. all sections begin at 0. */
  2916. for (asection *sect : gdb_bfd_sections (abfd))
  2917. {
  2918. sect->output_section = sect;
  2919. sect->output_offset = 0;
  2920. }
  2921. return bfd_simple_get_relocated_section_contents (abfd, sectp, buf, NULL);
  2922. }
  2923. /* Relocate the contents of a debug section SECTP in ABFD. The
  2924. contents are stored in BUF if it is non-NULL, or returned in a
  2925. malloc'd buffer otherwise.
  2926. For some platforms and debug info formats, shared libraries contain
  2927. relocations against the debug sections (particularly for DWARF-2;
  2928. one affected platform is PowerPC GNU/Linux, although it depends on
  2929. the version of the linker in use). Also, ELF object files naturally
  2930. have unresolved relocations for their debug sections. We need to apply
  2931. the relocations in order to get the locations of symbols correct.
  2932. Another example that may require relocation processing, is the
  2933. DWARF-2 .eh_frame section in .o files, although it isn't strictly a
  2934. debug section. */
  2935. bfd_byte *
  2936. symfile_relocate_debug_section (struct objfile *objfile,
  2937. asection *sectp, bfd_byte *buf)
  2938. {
  2939. gdb_assert (objfile->sf->sym_relocate);
  2940. return (*objfile->sf->sym_relocate) (objfile, sectp, buf);
  2941. }
  2942. symfile_segment_data_up
  2943. get_symfile_segment_data (bfd *abfd)
  2944. {
  2945. const struct sym_fns *sf = find_sym_fns (abfd);
  2946. if (sf == NULL)
  2947. return NULL;
  2948. return sf->sym_segments (abfd);
  2949. }
  2950. /* Given:
  2951. - DATA, containing segment addresses from the object file ABFD, and
  2952. the mapping from ABFD's sections onto the segments that own them,
  2953. and
  2954. - SEGMENT_BASES[0 .. NUM_SEGMENT_BASES - 1], holding the actual
  2955. segment addresses reported by the target,
  2956. store the appropriate offsets for each section in OFFSETS.
  2957. If there are fewer entries in SEGMENT_BASES than there are segments
  2958. in DATA, then apply SEGMENT_BASES' last entry to all the segments.
  2959. If there are more entries, then ignore the extra. The target may
  2960. not be able to distinguish between an empty data segment and a
  2961. missing data segment; a missing text segment is less plausible. */
  2962. int
  2963. symfile_map_offsets_to_segments (bfd *abfd,
  2964. const struct symfile_segment_data *data,
  2965. section_offsets &offsets,
  2966. int num_segment_bases,
  2967. const CORE_ADDR *segment_bases)
  2968. {
  2969. int i;
  2970. asection *sect;
  2971. /* It doesn't make sense to call this function unless you have some
  2972. segment base addresses. */
  2973. gdb_assert (num_segment_bases > 0);
  2974. /* If we do not have segment mappings for the object file, we
  2975. can not relocate it by segments. */
  2976. gdb_assert (data != NULL);
  2977. gdb_assert (data->segments.size () > 0);
  2978. for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
  2979. {
  2980. int which = data->segment_info[i];
  2981. gdb_assert (0 <= which && which <= data->segments.size ());
  2982. /* Don't bother computing offsets for sections that aren't
  2983. loaded as part of any segment. */
  2984. if (! which)
  2985. continue;
  2986. /* Use the last SEGMENT_BASES entry as the address of any extra
  2987. segments mentioned in DATA->segment_info. */
  2988. if (which > num_segment_bases)
  2989. which = num_segment_bases;
  2990. offsets[i] = segment_bases[which - 1] - data->segments[which - 1].base;
  2991. }
  2992. return 1;
  2993. }
  2994. static void
  2995. symfile_find_segment_sections (struct objfile *objfile)
  2996. {
  2997. bfd *abfd = objfile->obfd;
  2998. int i;
  2999. asection *sect;
  3000. symfile_segment_data_up data
  3001. = get_symfile_segment_data (objfile->obfd);
  3002. if (data == NULL)
  3003. return;
  3004. if (data->segments.size () != 1 && data->segments.size () != 2)
  3005. return;
  3006. for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
  3007. {
  3008. int which = data->segment_info[i];
  3009. if (which == 1)
  3010. {
  3011. if (objfile->sect_index_text == -1)
  3012. objfile->sect_index_text = sect->index;
  3013. if (objfile->sect_index_rodata == -1)
  3014. objfile->sect_index_rodata = sect->index;
  3015. }
  3016. else if (which == 2)
  3017. {
  3018. if (objfile->sect_index_data == -1)
  3019. objfile->sect_index_data = sect->index;
  3020. if (objfile->sect_index_bss == -1)
  3021. objfile->sect_index_bss = sect->index;
  3022. }
  3023. }
  3024. }
  3025. /* Listen for free_objfile events. */
  3026. static void
  3027. symfile_free_objfile (struct objfile *objfile)
  3028. {
  3029. /* Remove the target sections owned by this objfile. */
  3030. if (objfile != NULL)
  3031. current_program_space->remove_target_sections ((void *) objfile);
  3032. }
  3033. /* Wrapper around the quick_symbol_functions expand_symtabs_matching "method".
  3034. Expand all symtabs that match the specified criteria.
  3035. See quick_symbol_functions.expand_symtabs_matching for details. */
  3036. bool
  3037. expand_symtabs_matching
  3038. (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
  3039. const lookup_name_info &lookup_name,
  3040. gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
  3041. gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
  3042. block_search_flags search_flags,
  3043. enum search_domain kind)
  3044. {
  3045. for (objfile *objfile : current_program_space->objfiles ())
  3046. if (!objfile->expand_symtabs_matching (file_matcher,
  3047. &lookup_name,
  3048. symbol_matcher,
  3049. expansion_notify,
  3050. search_flags,
  3051. UNDEF_DOMAIN,
  3052. kind))
  3053. return false;
  3054. return true;
  3055. }
  3056. /* Wrapper around the quick_symbol_functions map_symbol_filenames "method".
  3057. Map function FUN over every file.
  3058. See quick_symbol_functions.map_symbol_filenames for details. */
  3059. void
  3060. map_symbol_filenames (gdb::function_view<symbol_filename_ftype> fun,
  3061. bool need_fullname)
  3062. {
  3063. for (objfile *objfile : current_program_space->objfiles ())
  3064. objfile->map_symbol_filenames (fun, need_fullname);
  3065. }
  3066. #if GDB_SELF_TEST
  3067. namespace selftests {
  3068. namespace filename_language {
  3069. static void test_filename_language ()
  3070. {
  3071. /* This test messes up the filename_language_table global. */
  3072. scoped_restore restore_flt = make_scoped_restore (&filename_language_table);
  3073. /* Test deducing an unknown extension. */
  3074. language lang = deduce_language_from_filename ("myfile.blah");
  3075. SELF_CHECK (lang == language_unknown);
  3076. /* Test deducing a known extension. */
  3077. lang = deduce_language_from_filename ("myfile.c");
  3078. SELF_CHECK (lang == language_c);
  3079. /* Test adding a new extension using the internal API. */
  3080. add_filename_language (".blah", language_pascal);
  3081. lang = deduce_language_from_filename ("myfile.blah");
  3082. SELF_CHECK (lang == language_pascal);
  3083. }
  3084. static void
  3085. test_set_ext_lang_command ()
  3086. {
  3087. /* This test messes up the filename_language_table global. */
  3088. scoped_restore restore_flt = make_scoped_restore (&filename_language_table);
  3089. /* Confirm that the .hello extension is not known. */
  3090. language lang = deduce_language_from_filename ("cake.hello");
  3091. SELF_CHECK (lang == language_unknown);
  3092. /* Test adding a new extension using the CLI command. */
  3093. ext_args = ".hello rust";
  3094. set_ext_lang_command (NULL, 1, NULL);
  3095. lang = deduce_language_from_filename ("cake.hello");
  3096. SELF_CHECK (lang == language_rust);
  3097. /* Test overriding an existing extension using the CLI command. */
  3098. int size_before = filename_language_table.size ();
  3099. ext_args = ".hello pascal";
  3100. set_ext_lang_command (NULL, 1, NULL);
  3101. int size_after = filename_language_table.size ();
  3102. lang = deduce_language_from_filename ("cake.hello");
  3103. SELF_CHECK (lang == language_pascal);
  3104. SELF_CHECK (size_before == size_after);
  3105. }
  3106. } /* namespace filename_language */
  3107. } /* namespace selftests */
  3108. #endif /* GDB_SELF_TEST */
  3109. void _initialize_symfile ();
  3110. void
  3111. _initialize_symfile ()
  3112. {
  3113. struct cmd_list_element *c;
  3114. gdb::observers::free_objfile.attach (symfile_free_objfile, "symfile");
  3115. #define READNOW_READNEVER_HELP \
  3116. "The '-readnow' option will cause GDB to read the entire symbol file\n\
  3117. immediately. This makes the command slower, but may make future operations\n\
  3118. faster.\n\
  3119. The '-readnever' option will prevent GDB from reading the symbol file's\n\
  3120. symbolic debug information."
  3121. c = add_cmd ("symbol-file", class_files, symbol_file_command, _("\
  3122. Load symbol table from executable file FILE.\n\
  3123. Usage: symbol-file [-readnow | -readnever] [-o OFF] FILE\n\
  3124. OFF is an optional offset which is added to each section address.\n\
  3125. The `file' command can also load symbol tables, as well as setting the file\n\
  3126. to execute.\n" READNOW_READNEVER_HELP), &cmdlist);
  3127. set_cmd_completer (c, filename_completer);
  3128. c = add_cmd ("add-symbol-file", class_files, add_symbol_file_command, _("\
  3129. Load symbols from FILE, assuming FILE has been dynamically loaded.\n\
  3130. Usage: add-symbol-file FILE [-readnow | -readnever] [-o OFF] [ADDR] \
  3131. [-s SECT-NAME SECT-ADDR]...\n\
  3132. ADDR is the starting address of the file's text.\n\
  3133. Each '-s' argument provides a section name and address, and\n\
  3134. should be specified if the data and bss segments are not contiguous\n\
  3135. with the text. SECT-NAME is a section name to be loaded at SECT-ADDR.\n\
  3136. OFF is an optional offset which is added to the default load addresses\n\
  3137. of all sections for which no other address was specified.\n"
  3138. READNOW_READNEVER_HELP),
  3139. &cmdlist);
  3140. set_cmd_completer (c, filename_completer);
  3141. c = add_cmd ("remove-symbol-file", class_files,
  3142. remove_symbol_file_command, _("\
  3143. Remove a symbol file added via the add-symbol-file command.\n\
  3144. Usage: remove-symbol-file FILENAME\n\
  3145. remove-symbol-file -a ADDRESS\n\
  3146. The file to remove can be identified by its filename or by an address\n\
  3147. that lies within the boundaries of this symbol file in memory."),
  3148. &cmdlist);
  3149. c = add_cmd ("load", class_files, load_command, _("\
  3150. Dynamically load FILE into the running program.\n\
  3151. FILE symbols are recorded for access from GDB.\n\
  3152. Usage: load [FILE] [OFFSET]\n\
  3153. An optional load OFFSET may also be given as a literal address.\n\
  3154. When OFFSET is provided, FILE must also be provided. FILE can be provided\n\
  3155. on its own."), &cmdlist);
  3156. set_cmd_completer (c, filename_completer);
  3157. cmd_list_element *overlay_cmd
  3158. = add_basic_prefix_cmd ("overlay", class_support,
  3159. _("Commands for debugging overlays."), &overlaylist,
  3160. 0, &cmdlist);
  3161. add_com_alias ("ovly", overlay_cmd, class_support, 1);
  3162. add_com_alias ("ov", overlay_cmd, class_support, 1);
  3163. add_cmd ("map-overlay", class_support, map_overlay_command,
  3164. _("Assert that an overlay section is mapped."), &overlaylist);
  3165. add_cmd ("unmap-overlay", class_support, unmap_overlay_command,
  3166. _("Assert that an overlay section is unmapped."), &overlaylist);
  3167. add_cmd ("list-overlays", class_support, list_overlays_command,
  3168. _("List mappings of overlay sections."), &overlaylist);
  3169. add_cmd ("manual", class_support, overlay_manual_command,
  3170. _("Enable overlay debugging."), &overlaylist);
  3171. add_cmd ("off", class_support, overlay_off_command,
  3172. _("Disable overlay debugging."), &overlaylist);
  3173. add_cmd ("auto", class_support, overlay_auto_command,
  3174. _("Enable automatic overlay debugging."), &overlaylist);
  3175. add_cmd ("load-target", class_support, overlay_load_command,
  3176. _("Read the overlay mapping state from the target."), &overlaylist);
  3177. /* Filename extension to source language lookup table: */
  3178. add_setshow_string_noescape_cmd ("extension-language", class_files,
  3179. &ext_args, _("\
  3180. Set mapping between filename extension and source language."), _("\
  3181. Show mapping between filename extension and source language."), _("\
  3182. Usage: set extension-language .foo bar"),
  3183. set_ext_lang_command,
  3184. show_ext_args,
  3185. &setlist, &showlist);
  3186. add_info ("extensions", info_ext_lang_command,
  3187. _("All filename extensions associated with a source language."));
  3188. add_setshow_optional_filename_cmd ("debug-file-directory", class_support,
  3189. &debug_file_directory, _("\
  3190. Set the directories where separate debug symbols are searched for."), _("\
  3191. Show the directories where separate debug symbols are searched for."), _("\
  3192. Separate debug symbols are first searched for in the same\n\
  3193. directory as the binary, then in the `" DEBUG_SUBDIRECTORY "' subdirectory,\n\
  3194. and lastly at the path of the directory of the binary with\n\
  3195. each global debug-file-directory component prepended."),
  3196. NULL,
  3197. show_debug_file_directory,
  3198. &setlist, &showlist);
  3199. add_setshow_enum_cmd ("symbol-loading", no_class,
  3200. print_symbol_loading_enums, &print_symbol_loading,
  3201. _("\
  3202. Set printing of symbol loading messages."), _("\
  3203. Show printing of symbol loading messages."), _("\
  3204. off == turn all messages off\n\
  3205. brief == print messages for the executable,\n\
  3206. and brief messages for shared libraries\n\
  3207. full == print messages for the executable,\n\
  3208. and messages for each shared library."),
  3209. NULL,
  3210. NULL,
  3211. &setprintlist, &showprintlist);
  3212. add_setshow_boolean_cmd ("separate-debug-file", no_class,
  3213. &separate_debug_file_debug, _("\
  3214. Set printing of separate debug info file search debug."), _("\
  3215. Show printing of separate debug info file search debug."), _("\
  3216. When on, GDB prints the searched locations while looking for separate debug \
  3217. info files."), NULL, NULL, &setdebuglist, &showdebuglist);
  3218. #if GDB_SELF_TEST
  3219. selftests::register_test
  3220. ("filename_language", selftests::filename_language::test_filename_language);
  3221. selftests::register_test
  3222. ("set_ext_lang_command",
  3223. selftests::filename_language::test_set_ext_lang_command);
  3224. #endif
  3225. }