xcoffread.c 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088
  1. /* Read AIX xcoff symbol tables and convert to internal format, for GDB.
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. Derived from coffread.c, dbxread.c, and a lot of hacking.
  4. Contributed by IBM Corporation.
  5. This file is part of GDB.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #include "defs.h"
  17. #include "bfd.h"
  18. #include <sys/types.h>
  19. #include <fcntl.h>
  20. #include <ctype.h>
  21. #ifdef HAVE_SYS_FILE_H
  22. #include <sys/file.h>
  23. #endif
  24. #include <sys/stat.h>
  25. #include <algorithm>
  26. #include "coff/internal.h"
  27. #include "libcoff.h" /* FIXME, internal data from BFD */
  28. #include "coff/xcoff.h"
  29. #include "libxcoff.h"
  30. #include "coff/rs6000.h"
  31. #include "xcoffread.h"
  32. #include "symtab.h"
  33. #include "gdbtypes.h"
  34. /* FIXME: ezannoni/2004-02-13 Verify if the include below is really needed. */
  35. #include "symfile.h"
  36. #include "objfiles.h"
  37. #include "buildsym-legacy.h"
  38. #include "stabsread.h"
  39. #include "expression.h"
  40. #include "complaints.h"
  41. #include "psympriv.h"
  42. #include "dwarf2/sect-names.h"
  43. #include "dwarf2/public.h"
  44. #include "gdb-stabs.h"
  45. /* For interface with stabsread.c. */
  46. #include "aout/stab_gnu.h"
  47. /* We put a pointer to this structure in the read_symtab_private field
  48. of the psymtab. */
  49. struct symloc
  50. {
  51. /* First symbol number for this file. */
  52. int first_symnum;
  53. /* Number of symbols in the section of the symbol table devoted to
  54. this file's symbols (actually, the section bracketed may contain
  55. more than just this file's symbols). If numsyms is 0, the only
  56. reason for this thing's existence is the dependency list. Nothing
  57. else will happen when it is read in. */
  58. int numsyms;
  59. /* Position of the start of the line number information for this
  60. psymtab. */
  61. unsigned int lineno_off;
  62. };
  63. /* Remember what we deduced to be the source language of this psymtab. */
  64. static enum language psymtab_language = language_unknown;
  65. /* Simplified internal version of coff symbol table information. */
  66. struct coff_symbol
  67. {
  68. char *c_name;
  69. int c_symnum; /* Symbol number of this entry. */
  70. int c_naux; /* 0 if syment only, 1 if syment + auxent. */
  71. CORE_ADDR c_value;
  72. unsigned char c_sclass;
  73. int c_secnum;
  74. unsigned int c_type;
  75. };
  76. /* Last function's saved coff symbol `cs'. */
  77. static struct coff_symbol fcn_cs_saved;
  78. static bfd *symfile_bfd;
  79. /* Core address of start and end of text of current source file.
  80. This is calculated from the first function seen after a C_FILE
  81. symbol. */
  82. static CORE_ADDR cur_src_end_addr;
  83. /* Core address of the end of the first object file. */
  84. static CORE_ADDR first_object_file_end;
  85. /* Initial symbol-table-debug-string vector length. */
  86. #define INITIAL_STABVECTOR_LENGTH 40
  87. /* Size of a COFF symbol. I think it is always 18, so I'm not sure
  88. there is any reason not to just use a #define, but might as well
  89. ask BFD for the size and store it here, I guess. */
  90. static unsigned local_symesz;
  91. struct xcoff_symfile_info
  92. {
  93. file_ptr min_lineno_offset {}; /* Where in file lowest line#s are. */
  94. file_ptr max_lineno_offset {}; /* 1+last byte of line#s in file. */
  95. /* Pointer to the string table. */
  96. char *strtbl = nullptr;
  97. /* Pointer to debug section. */
  98. char *debugsec = nullptr;
  99. /* Pointer to the a.out symbol table. */
  100. char *symtbl = nullptr;
  101. /* Number of symbols in symtbl. */
  102. int symtbl_num_syms = 0;
  103. /* Offset in data section to TOC anchor. */
  104. CORE_ADDR toc_offset = 0;
  105. };
  106. /* Key for XCOFF-associated data. */
  107. static const struct objfile_key<xcoff_symfile_info> xcoff_objfile_data_key;
  108. /* Convenience macro to access the per-objfile XCOFF data. */
  109. #define XCOFF_DATA(objfile) \
  110. xcoff_objfile_data_key.get (objfile)
  111. /* XCOFF names for dwarf sections. There is no compressed sections. */
  112. static const struct dwarf2_debug_sections dwarf2_xcoff_names = {
  113. { ".dwinfo", NULL },
  114. { ".dwabrev", NULL },
  115. { ".dwline", NULL },
  116. { ".dwloc", NULL },
  117. { NULL, NULL }, /* debug_loclists */
  118. /* AIX XCOFF defines one, named DWARF section for macro debug information.
  119. XLC does not generate debug_macinfo for DWARF4 and below.
  120. The section is assigned to debug_macro for DWARF5 and above. */
  121. { NULL, NULL },
  122. { ".dwmac", NULL },
  123. { ".dwstr", NULL },
  124. { NULL, NULL }, /* debug_str_offsets */
  125. { NULL, NULL }, /* debug_line_str */
  126. { ".dwrnges", NULL },
  127. { NULL, NULL }, /* debug_rnglists */
  128. { ".dwpbtyp", NULL },
  129. { NULL, NULL }, /* debug_addr */
  130. { ".dwframe", NULL },
  131. { NULL, NULL }, /* eh_frame */
  132. { NULL, NULL }, /* gdb_index */
  133. { NULL, NULL }, /* debug_names */
  134. { NULL, NULL }, /* debug_aranges */
  135. 23
  136. };
  137. static void
  138. bf_notfound_complaint (void)
  139. {
  140. complaint (_("line numbers off, `.bf' symbol not found"));
  141. }
  142. static void
  143. ef_complaint (int arg1)
  144. {
  145. complaint (_("Mismatched .ef symbol ignored starting at symnum %d"), arg1);
  146. }
  147. static void
  148. eb_complaint (int arg1)
  149. {
  150. complaint (_("Mismatched .eb symbol ignored starting at symnum %d"), arg1);
  151. }
  152. static void xcoff_initial_scan (struct objfile *, symfile_add_flags);
  153. static void scan_xcoff_symtab (minimal_symbol_reader &,
  154. psymtab_storage *partial_symtabs,
  155. struct objfile *);
  156. static const char *xcoff_next_symbol_text (struct objfile *);
  157. static void record_include_begin (struct coff_symbol *);
  158. static void
  159. enter_line_range (struct subfile *, unsigned, unsigned,
  160. CORE_ADDR, CORE_ADDR, unsigned *);
  161. static void init_stringtab (bfd *, file_ptr, struct objfile *);
  162. static void xcoff_symfile_init (struct objfile *);
  163. static void xcoff_new_init (struct objfile *);
  164. static void xcoff_symfile_finish (struct objfile *);
  165. static char *coff_getfilename (union internal_auxent *, struct objfile *);
  166. static void read_symbol (struct internal_syment *, int);
  167. static int read_symbol_lineno (int);
  168. static CORE_ADDR read_symbol_nvalue (int);
  169. static struct symbol *process_xcoff_symbol (struct coff_symbol *,
  170. struct objfile *);
  171. static void read_xcoff_symtab (struct objfile *, legacy_psymtab *);
  172. #if 0
  173. static void add_stab_to_list (char *, struct pending_stabs **);
  174. #endif
  175. static struct linetable *arrange_linetable (struct linetable *);
  176. static void record_include_end (struct coff_symbol *);
  177. static void process_linenos (CORE_ADDR, CORE_ADDR);
  178. /* Translate from a COFF section number (target_index) to a SECT_OFF_*
  179. code. */
  180. static int secnum_to_section (int, struct objfile *);
  181. static asection *secnum_to_bfd_section (int, struct objfile *);
  182. struct find_targ_sec_arg
  183. {
  184. int targ_index;
  185. int *resultp;
  186. asection **bfd_sect;
  187. struct objfile *objfile;
  188. };
  189. static void find_targ_sec (bfd *, asection *, void *);
  190. static void
  191. find_targ_sec (bfd *abfd, asection *sect, void *obj)
  192. {
  193. struct find_targ_sec_arg *args = (struct find_targ_sec_arg *) obj;
  194. struct objfile *objfile = args->objfile;
  195. if (sect->target_index == args->targ_index)
  196. {
  197. /* This is the section. Figure out what SECT_OFF_* code it is. */
  198. if (bfd_section_flags (sect) & SEC_CODE)
  199. *args->resultp = SECT_OFF_TEXT (objfile);
  200. else if (bfd_section_flags (sect) & SEC_LOAD)
  201. *args->resultp = SECT_OFF_DATA (objfile);
  202. else
  203. *args->resultp = gdb_bfd_section_index (abfd, sect);
  204. *args->bfd_sect = sect;
  205. }
  206. }
  207. /* Search all BFD sections for the section whose target_index is
  208. equal to N_SCNUM. Set *BFD_SECT to that section. The section's
  209. associated index in the objfile's section_offset table is also
  210. stored in *SECNUM.
  211. If no match is found, *BFD_SECT is set to NULL, and *SECNUM
  212. is set to the text section's number. */
  213. static void
  214. xcoff_secnum_to_sections (int n_scnum, struct objfile *objfile,
  215. asection **bfd_sect, int *secnum)
  216. {
  217. struct find_targ_sec_arg args;
  218. args.targ_index = n_scnum;
  219. args.resultp = secnum;
  220. args.bfd_sect = bfd_sect;
  221. args.objfile = objfile;
  222. *bfd_sect = NULL;
  223. *secnum = SECT_OFF_TEXT (objfile);
  224. bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
  225. }
  226. /* Return the section number (SECT_OFF_*) that N_SCNUM points to. */
  227. static int
  228. secnum_to_section (int n_scnum, struct objfile *objfile)
  229. {
  230. int secnum;
  231. asection *ignored;
  232. xcoff_secnum_to_sections (n_scnum, objfile, &ignored, &secnum);
  233. return secnum;
  234. }
  235. /* Return the BFD section that N_SCNUM points to. */
  236. static asection *
  237. secnum_to_bfd_section (int n_scnum, struct objfile *objfile)
  238. {
  239. int ignored;
  240. asection *bfd_sect;
  241. xcoff_secnum_to_sections (n_scnum, objfile, &bfd_sect, &ignored);
  242. return bfd_sect;
  243. }
  244. /* add a given stab string into given stab vector. */
  245. #if 0
  246. static void
  247. add_stab_to_list (char *stabname, struct pending_stabs **stabvector)
  248. {
  249. if (*stabvector == NULL)
  250. {
  251. *stabvector = (struct pending_stabs *)
  252. xmalloc (sizeof (struct pending_stabs) +
  253. INITIAL_STABVECTOR_LENGTH * sizeof (char *));
  254. (*stabvector)->count = 0;
  255. (*stabvector)->length = INITIAL_STABVECTOR_LENGTH;
  256. }
  257. else if ((*stabvector)->count >= (*stabvector)->length)
  258. {
  259. (*stabvector)->length += INITIAL_STABVECTOR_LENGTH;
  260. *stabvector = (struct pending_stabs *)
  261. xrealloc ((char *) *stabvector, sizeof (struct pending_stabs) +
  262. (*stabvector)->length * sizeof (char *));
  263. }
  264. (*stabvector)->stab[(*stabvector)->count++] = stabname;
  265. }
  266. #endif
  267. /* *INDENT-OFF* */
  268. /* Linenos are processed on a file-by-file basis.
  269. Two reasons:
  270. 1) xlc (IBM's native c compiler) postpones static function code
  271. emission to the end of a compilation unit. This way it can
  272. determine if those functions (statics) are needed or not, and
  273. can do some garbage collection (I think). This makes line
  274. numbers and corresponding addresses unordered, and we end up
  275. with a line table like:
  276. lineno addr
  277. foo() 10 0x100
  278. 20 0x200
  279. 30 0x300
  280. foo3() 70 0x400
  281. 80 0x500
  282. 90 0x600
  283. static foo2()
  284. 40 0x700
  285. 50 0x800
  286. 60 0x900
  287. and that breaks gdb's binary search on line numbers, if the
  288. above table is not sorted on line numbers. And that sort
  289. should be on function based, since gcc can emit line numbers
  290. like:
  291. 10 0x100 - for the init/test part of a for stmt.
  292. 20 0x200
  293. 30 0x300
  294. 10 0x400 - for the increment part of a for stmt.
  295. arrange_linetable() will do this sorting.
  296. 2) aix symbol table might look like:
  297. c_file // beginning of a new file
  298. .bi // beginning of include file
  299. .ei // end of include file
  300. .bi
  301. .ei
  302. basically, .bi/.ei pairs do not necessarily encapsulate
  303. their scope. They need to be recorded, and processed later
  304. on when we come the end of the compilation unit.
  305. Include table (inclTable) and process_linenos() handle
  306. that. */
  307. /* *INDENT-ON* */
  308. /* Given a line table with function entries are marked, arrange its
  309. functions in ascending order and strip off function entry markers
  310. and return it in a newly created table. If the old one is good
  311. enough, return the old one. */
  312. /* FIXME: I think all this stuff can be replaced by just passing
  313. sort_linevec = 1 to end_compunit_symtab. */
  314. static struct linetable *
  315. arrange_linetable (struct linetable *oldLineTb)
  316. {
  317. int ii, jj, newline, /* new line count */
  318. function_count; /* # of functions */
  319. struct linetable_entry *fentry; /* function entry vector */
  320. int fentry_size; /* # of function entries */
  321. struct linetable *newLineTb; /* new line table */
  322. int extra_lines = 0;
  323. #define NUM_OF_FUNCTIONS 20
  324. fentry_size = NUM_OF_FUNCTIONS;
  325. fentry = XNEWVEC (struct linetable_entry, fentry_size);
  326. for (function_count = 0, ii = 0; ii < oldLineTb->nitems; ++ii)
  327. {
  328. if (oldLineTb->item[ii].is_stmt == 0)
  329. continue;
  330. if (oldLineTb->item[ii].line == 0)
  331. { /* Function entry found. */
  332. if (function_count >= fentry_size)
  333. { /* Make sure you have room. */
  334. fentry_size *= 2;
  335. fentry = (struct linetable_entry *)
  336. xrealloc (fentry,
  337. fentry_size * sizeof (struct linetable_entry));
  338. }
  339. fentry[function_count].line = ii;
  340. fentry[function_count].is_stmt = 1;
  341. fentry[function_count].pc = oldLineTb->item[ii].pc;
  342. ++function_count;
  343. /* If the function was compiled with XLC, we may have to add an
  344. extra line entry later. Reserve space for that. */
  345. if (ii + 1 < oldLineTb->nitems
  346. && oldLineTb->item[ii].pc != oldLineTb->item[ii + 1].pc)
  347. extra_lines++;
  348. }
  349. }
  350. if (function_count == 0)
  351. {
  352. xfree (fentry);
  353. return oldLineTb;
  354. }
  355. else if (function_count > 1)
  356. std::sort (fentry, fentry + function_count,
  357. [] (const linetable_entry &lte1, const linetable_entry& lte2)
  358. { return lte1.pc < lte2.pc; });
  359. /* Allocate a new line table. */
  360. newLineTb = (struct linetable *)
  361. xmalloc
  362. (sizeof (struct linetable) +
  363. (oldLineTb->nitems - function_count + extra_lines) * sizeof (struct linetable_entry));
  364. /* If line table does not start with a function beginning, copy up until
  365. a function begin. */
  366. newline = 0;
  367. if (oldLineTb->item[0].line != 0)
  368. for (newline = 0;
  369. newline < oldLineTb->nitems && oldLineTb->item[newline].line;
  370. ++newline)
  371. newLineTb->item[newline] = oldLineTb->item[newline];
  372. /* Now copy function lines one by one. */
  373. for (ii = 0; ii < function_count; ++ii)
  374. {
  375. /* If the function was compiled with XLC, we may have to add an
  376. extra line to cover the function prologue. */
  377. jj = fentry[ii].line;
  378. if (jj + 1 < oldLineTb->nitems
  379. && oldLineTb->item[jj].pc != oldLineTb->item[jj + 1].pc)
  380. {
  381. newLineTb->item[newline] = oldLineTb->item[jj];
  382. newLineTb->item[newline].line = oldLineTb->item[jj + 1].line;
  383. newline++;
  384. }
  385. for (jj = fentry[ii].line + 1;
  386. jj < oldLineTb->nitems && oldLineTb->item[jj].line != 0;
  387. ++jj, ++newline)
  388. newLineTb->item[newline] = oldLineTb->item[jj];
  389. }
  390. xfree (fentry);
  391. /* The number of items in the line table must include these
  392. extra lines which were added in case of XLC compiled functions. */
  393. newLineTb->nitems = oldLineTb->nitems - function_count + extra_lines;
  394. return newLineTb;
  395. }
  396. /* include file support: C_BINCL/C_EINCL pairs will be kept in the
  397. following `IncludeChain'. At the end of each symtab (end_compunit_symtab),
  398. we will determine if we should create additional symtab's to
  399. represent if (the include files. */
  400. typedef struct _inclTable
  401. {
  402. char *name; /* include filename */
  403. /* Offsets to the line table. end points to the last entry which is
  404. part of this include file. */
  405. int begin, end;
  406. struct subfile *subfile;
  407. unsigned funStartLine; /* Start line # of its function. */
  408. }
  409. InclTable;
  410. #define INITIAL_INCLUDE_TABLE_LENGTH 20
  411. static InclTable *inclTable; /* global include table */
  412. static int inclIndx; /* last entry to table */
  413. static int inclLength; /* table length */
  414. static int inclDepth; /* nested include depth */
  415. static void allocate_include_entry (void);
  416. static void
  417. record_include_begin (struct coff_symbol *cs)
  418. {
  419. if (inclDepth)
  420. {
  421. /* In xcoff, we assume include files cannot be nested (not in .c files
  422. of course, but in corresponding .s files.). */
  423. /* This can happen with old versions of GCC.
  424. GCC 2.3.3-930426 does not exhibit this on a test case which
  425. a user said produced the message for him. */
  426. complaint (_("Nested C_BINCL symbols"));
  427. }
  428. ++inclDepth;
  429. allocate_include_entry ();
  430. inclTable[inclIndx].name = cs->c_name;
  431. inclTable[inclIndx].begin = cs->c_value;
  432. }
  433. static void
  434. record_include_end (struct coff_symbol *cs)
  435. {
  436. InclTable *pTbl;
  437. if (inclDepth == 0)
  438. {
  439. complaint (_("Mismatched C_BINCL/C_EINCL pair"));
  440. }
  441. allocate_include_entry ();
  442. pTbl = &inclTable[inclIndx];
  443. pTbl->end = cs->c_value;
  444. --inclDepth;
  445. ++inclIndx;
  446. }
  447. static void
  448. allocate_include_entry (void)
  449. {
  450. if (inclTable == NULL)
  451. {
  452. inclTable = XCNEWVEC (InclTable, INITIAL_INCLUDE_TABLE_LENGTH);
  453. inclLength = INITIAL_INCLUDE_TABLE_LENGTH;
  454. inclIndx = 0;
  455. }
  456. else if (inclIndx >= inclLength)
  457. {
  458. inclLength += INITIAL_INCLUDE_TABLE_LENGTH;
  459. inclTable = XRESIZEVEC (InclTable, inclTable, inclLength);
  460. memset (inclTable + inclLength - INITIAL_INCLUDE_TABLE_LENGTH,
  461. '\0', sizeof (InclTable) * INITIAL_INCLUDE_TABLE_LENGTH);
  462. }
  463. }
  464. /* Global variable to pass the psymtab down to all the routines involved
  465. in psymtab to symtab processing. */
  466. static legacy_psymtab *this_symtab_psymtab;
  467. /* Objfile related to this_symtab_psymtab; set at the same time. */
  468. static struct objfile *this_symtab_objfile;
  469. /* given the start and end addresses of a compilation unit (or a csect,
  470. at times) process its lines and create appropriate line vectors. */
  471. static void
  472. process_linenos (CORE_ADDR start, CORE_ADDR end)
  473. {
  474. int offset, ii;
  475. file_ptr max_offset
  476. = XCOFF_DATA (this_symtab_objfile)->max_lineno_offset;
  477. /* subfile structure for the main compilation unit. */
  478. struct subfile main_subfile;
  479. /* In the main source file, any time we see a function entry, we
  480. reset this variable to function's absolute starting line number.
  481. All the following line numbers in the function are relative to
  482. this, and we record absolute line numbers in record_line(). */
  483. unsigned int main_source_baseline = 0;
  484. unsigned *firstLine;
  485. offset =
  486. ((struct symloc *) this_symtab_psymtab->read_symtab_private)->lineno_off;
  487. if (offset == 0)
  488. goto return_after_cleanup;
  489. memset (&main_subfile, '\0', sizeof (main_subfile));
  490. if (inclIndx == 0)
  491. /* All source lines were in the main source file. None in include
  492. files. */
  493. enter_line_range (&main_subfile, offset, 0, start, end,
  494. &main_source_baseline);
  495. else
  496. {
  497. /* There was source with line numbers in include files. */
  498. int linesz =
  499. coff_data (this_symtab_objfile->obfd)->local_linesz;
  500. main_source_baseline = 0;
  501. for (ii = 0; ii < inclIndx; ++ii)
  502. {
  503. struct subfile *tmpSubfile;
  504. /* If there is main file source before include file, enter it. */
  505. if (offset < inclTable[ii].begin)
  506. {
  507. enter_line_range
  508. (&main_subfile, offset, inclTable[ii].begin - linesz,
  509. start, 0, &main_source_baseline);
  510. }
  511. if (strcmp (inclTable[ii].name, get_last_source_file ()) == 0)
  512. {
  513. /* The entry in the include table refers to the main source
  514. file. Add the lines to the main subfile. */
  515. main_source_baseline = inclTable[ii].funStartLine;
  516. enter_line_range
  517. (&main_subfile, inclTable[ii].begin, inclTable[ii].end,
  518. start, 0, &main_source_baseline);
  519. inclTable[ii].subfile = &main_subfile;
  520. }
  521. else
  522. {
  523. /* Have a new subfile for the include file. */
  524. tmpSubfile = inclTable[ii].subfile = XNEW (struct subfile);
  525. memset (tmpSubfile, '\0', sizeof (struct subfile));
  526. firstLine = &(inclTable[ii].funStartLine);
  527. /* Enter include file's lines now. */
  528. enter_line_range (tmpSubfile, inclTable[ii].begin,
  529. inclTable[ii].end, start, 0, firstLine);
  530. }
  531. if (offset <= inclTable[ii].end)
  532. offset = inclTable[ii].end + linesz;
  533. }
  534. /* All the include files' line have been processed at this point. Now,
  535. enter remaining lines of the main file, if any left. */
  536. if (offset < max_offset + 1 - linesz)
  537. {
  538. enter_line_range (&main_subfile, offset, 0, start, end,
  539. &main_source_baseline);
  540. }
  541. }
  542. /* Process main file's line numbers. */
  543. if (main_subfile.line_vector)
  544. {
  545. struct linetable *lineTb, *lv;
  546. lv = main_subfile.line_vector;
  547. /* Line numbers are not necessarily ordered. xlc compilation will
  548. put static function to the end. */
  549. struct subfile *current_subfile = get_current_subfile ();
  550. lineTb = arrange_linetable (lv);
  551. if (lv == lineTb)
  552. {
  553. current_subfile->line_vector = (struct linetable *)
  554. xrealloc (lv, (sizeof (struct linetable)
  555. + lv->nitems * sizeof (struct linetable_entry)));
  556. }
  557. else
  558. {
  559. xfree (lv);
  560. current_subfile->line_vector = lineTb;
  561. }
  562. current_subfile->line_vector_length =
  563. current_subfile->line_vector->nitems;
  564. }
  565. /* Now, process included files' line numbers. */
  566. for (ii = 0; ii < inclIndx; ++ii)
  567. {
  568. if (inclTable[ii].subfile != ((struct subfile *) &main_subfile)
  569. && (inclTable[ii].subfile)->line_vector) /* Useless if!!!
  570. FIXMEmgo */
  571. {
  572. struct linetable *lineTb, *lv;
  573. lv = (inclTable[ii].subfile)->line_vector;
  574. /* Line numbers are not necessarily ordered. xlc compilation will
  575. put static function to the end. */
  576. lineTb = arrange_linetable (lv);
  577. push_subfile ();
  578. /* For the same include file, we might want to have more than one
  579. subfile. This happens if we have something like:
  580. ......
  581. #include "foo.h"
  582. ......
  583. #include "foo.h"
  584. ......
  585. while foo.h including code in it. (stupid but possible)
  586. Since start_subfile() looks at the name and uses an
  587. existing one if finds, we need to provide a fake name and
  588. fool it. */
  589. #if 0
  590. start_subfile (inclTable[ii].name);
  591. #else
  592. {
  593. /* Pick a fake name that will produce the same results as this
  594. one when passed to deduce_language_from_filename. Kludge on
  595. top of kludge. */
  596. const char *fakename = strrchr (inclTable[ii].name, '.');
  597. if (fakename == NULL)
  598. fakename = " ?";
  599. start_subfile (fakename);
  600. xfree (get_current_subfile ()->name);
  601. }
  602. struct subfile *current_subfile = get_current_subfile ();
  603. current_subfile->name = xstrdup (inclTable[ii].name);
  604. #endif
  605. if (lv == lineTb)
  606. {
  607. current_subfile->line_vector =
  608. (struct linetable *) xrealloc
  609. (lv, (sizeof (struct linetable)
  610. + lv->nitems * sizeof (struct linetable_entry)));
  611. }
  612. else
  613. {
  614. xfree (lv);
  615. current_subfile->line_vector = lineTb;
  616. }
  617. current_subfile->line_vector_length =
  618. current_subfile->line_vector->nitems;
  619. start_subfile (pop_subfile ());
  620. }
  621. }
  622. return_after_cleanup:
  623. /* We don't want to keep alloc/free'ing the global include file table. */
  624. inclIndx = 0;
  625. }
  626. static void
  627. aix_process_linenos (struct objfile *objfile)
  628. {
  629. /* There is no linenos to read if there are only dwarf info. */
  630. if (this_symtab_psymtab == NULL)
  631. return;
  632. /* Process line numbers and enter them into line vector. */
  633. process_linenos (get_last_source_start_addr (), cur_src_end_addr);
  634. }
  635. /* Enter a given range of lines into the line vector.
  636. can be called in the following two ways:
  637. enter_line_range (subfile, beginoffset, endoffset,
  638. startaddr, 0, firstLine) or
  639. enter_line_range (subfile, beginoffset, 0,
  640. startaddr, endaddr, firstLine)
  641. endoffset points to the last line table entry that we should pay
  642. attention to. */
  643. static void
  644. enter_line_range (struct subfile *subfile, unsigned beginoffset,
  645. unsigned endoffset, /* offsets to line table */
  646. CORE_ADDR startaddr, /* offsets to line table */
  647. CORE_ADDR endaddr, unsigned *firstLine)
  648. {
  649. struct objfile *objfile = this_symtab_objfile;
  650. struct gdbarch *gdbarch = objfile->arch ();
  651. unsigned int curoffset;
  652. CORE_ADDR addr;
  653. void *ext_lnno;
  654. struct internal_lineno int_lnno;
  655. unsigned int limit_offset;
  656. bfd *abfd;
  657. int linesz;
  658. if (endoffset == 0 && startaddr == 0 && endaddr == 0)
  659. return;
  660. curoffset = beginoffset;
  661. limit_offset = XCOFF_DATA (objfile)->max_lineno_offset;
  662. if (endoffset != 0)
  663. {
  664. if (endoffset >= limit_offset)
  665. {
  666. complaint (_("Bad line table offset in C_EINCL directive"));
  667. return;
  668. }
  669. limit_offset = endoffset;
  670. }
  671. else
  672. limit_offset -= 1;
  673. abfd = objfile->obfd;
  674. linesz = coff_data (abfd)->local_linesz;
  675. ext_lnno = alloca (linesz);
  676. while (curoffset <= limit_offset)
  677. {
  678. bfd_seek (abfd, curoffset, SEEK_SET);
  679. bfd_bread (ext_lnno, linesz, abfd);
  680. bfd_coff_swap_lineno_in (abfd, ext_lnno, &int_lnno);
  681. /* Find the address this line represents. */
  682. addr = (int_lnno.l_lnno
  683. ? int_lnno.l_addr.l_paddr
  684. : read_symbol_nvalue (int_lnno.l_addr.l_symndx));
  685. addr += objfile->text_section_offset ();
  686. if (addr < startaddr || (endaddr && addr >= endaddr))
  687. return;
  688. if (int_lnno.l_lnno == 0)
  689. {
  690. *firstLine = read_symbol_lineno (int_lnno.l_addr.l_symndx);
  691. record_line (subfile, 0, gdbarch_addr_bits_remove (gdbarch, addr));
  692. --(*firstLine);
  693. }
  694. else
  695. record_line (subfile, *firstLine + int_lnno.l_lnno,
  696. gdbarch_addr_bits_remove (gdbarch, addr));
  697. curoffset += linesz;
  698. }
  699. }
  700. /* Save the vital information for use when closing off the current file.
  701. NAME is the file name the symbols came from, START_ADDR is the first
  702. text address for the file, and SIZE is the number of bytes of text. */
  703. #define complete_symtab(name, start_addr) { \
  704. set_last_source_file (name); \
  705. set_last_source_start_addr (start_addr); \
  706. }
  707. /* Refill the symbol table input buffer
  708. and set the variables that control fetching entries from it.
  709. Reports an error if no data available.
  710. This function can read past the end of the symbol table
  711. (into the string table) but this does no harm. */
  712. /* Create a new minimal symbol (using record_with_info).
  713. Creation of all new minimal symbols should go through this function
  714. rather than calling the various record functions in order
  715. to make sure that all symbol addresses get properly relocated.
  716. Arguments are:
  717. NAME - the symbol's name (but if NAME starts with a period, that
  718. leading period is discarded).
  719. ADDRESS - the symbol's address, prior to relocation. This function
  720. relocates the address before recording the minimal symbol.
  721. MS_TYPE - the symbol's type.
  722. N_SCNUM - the symbol's XCOFF section number.
  723. OBJFILE - the objfile associated with the minimal symbol. */
  724. static void
  725. record_minimal_symbol (minimal_symbol_reader &reader,
  726. const char *name, CORE_ADDR address,
  727. enum minimal_symbol_type ms_type,
  728. int n_scnum,
  729. struct objfile *objfile)
  730. {
  731. if (name[0] == '.')
  732. ++name;
  733. reader.record_with_info (name, address, ms_type,
  734. secnum_to_section (n_scnum, objfile));
  735. }
  736. /* xcoff has static blocks marked in `.bs', `.es' pairs. They cannot be
  737. nested. At any given time, a symbol can only be in one static block.
  738. This is the base address of current static block, zero if non exists. */
  739. static int static_block_base = 0;
  740. /* Section number for the current static block. */
  741. static int static_block_section = -1;
  742. /* true if space for symbol name has been allocated. */
  743. static int symname_alloced = 0;
  744. /* Next symbol to read. Pointer into raw seething symbol table. */
  745. static char *raw_symbol;
  746. /* This is the function which stabsread.c calls to get symbol
  747. continuations. */
  748. static const char *
  749. xcoff_next_symbol_text (struct objfile *objfile)
  750. {
  751. struct internal_syment symbol;
  752. const char *retval;
  753. /* FIXME: is this the same as the passed arg? */
  754. if (this_symtab_objfile)
  755. objfile = this_symtab_objfile;
  756. bfd_coff_swap_sym_in (objfile->obfd, raw_symbol, &symbol);
  757. if (symbol.n_zeroes)
  758. {
  759. complaint (_("Unexpected symbol continuation"));
  760. /* Return something which points to '\0' and hope the symbol reading
  761. code does something reasonable. */
  762. retval = "";
  763. }
  764. else if (symbol.n_sclass & 0x80)
  765. {
  766. retval = XCOFF_DATA (objfile)->debugsec + symbol.n_offset;
  767. raw_symbol += coff_data (objfile->obfd)->local_symesz;
  768. ++symnum;
  769. }
  770. else
  771. {
  772. complaint (_("Unexpected symbol continuation"));
  773. /* Return something which points to '\0' and hope the symbol reading
  774. code does something reasonable. */
  775. retval = "";
  776. }
  777. return retval;
  778. }
  779. /* Read symbols for a given partial symbol table. */
  780. static void
  781. read_xcoff_symtab (struct objfile *objfile, legacy_psymtab *pst)
  782. {
  783. bfd *abfd = objfile->obfd;
  784. char *raw_auxptr; /* Pointer to first raw aux entry for sym. */
  785. struct xcoff_symfile_info *xcoff = XCOFF_DATA (objfile);
  786. char *strtbl = xcoff->strtbl;
  787. char *debugsec = xcoff->debugsec;
  788. const char *debugfmt = bfd_xcoff_is_xcoff64 (abfd) ? "XCOFF64" : "XCOFF";
  789. struct internal_syment symbol[1];
  790. union internal_auxent main_aux;
  791. struct coff_symbol cs[1];
  792. CORE_ADDR file_start_addr = 0;
  793. CORE_ADDR file_end_addr = 0;
  794. int next_file_symnum = -1;
  795. unsigned int max_symnum;
  796. int just_started = 1;
  797. int depth = 0;
  798. CORE_ADDR fcn_start_addr = 0;
  799. enum language pst_symtab_language;
  800. struct coff_symbol fcn_stab_saved = { 0 };
  801. /* fcn_cs_saved is global because process_xcoff_symbol needs it. */
  802. union internal_auxent fcn_aux_saved = main_aux;
  803. struct context_stack *newobj;
  804. const char *filestring = pst->filename; /* Name of the current file. */
  805. const char *last_csect_name; /* Last seen csect's name. */
  806. this_symtab_psymtab = pst;
  807. this_symtab_objfile = objfile;
  808. /* Get the appropriate COFF "constants" related to the file we're
  809. handling. */
  810. local_symesz = coff_data (abfd)->local_symesz;
  811. set_last_source_file (NULL);
  812. last_csect_name = 0;
  813. pst_symtab_language = deduce_language_from_filename (filestring);
  814. start_stabs ();
  815. start_compunit_symtab (objfile, filestring, NULL, file_start_addr,
  816. pst_symtab_language);
  817. record_debugformat (debugfmt);
  818. symnum = ((struct symloc *) pst->read_symtab_private)->first_symnum;
  819. max_symnum =
  820. symnum + ((struct symloc *) pst->read_symtab_private)->numsyms;
  821. first_object_file_end = 0;
  822. raw_symbol = xcoff->symtbl + symnum * local_symesz;
  823. while (symnum < max_symnum)
  824. {
  825. QUIT; /* make this command interruptable. */
  826. /* READ_ONE_SYMBOL (symbol, cs, symname_alloced); */
  827. /* read one symbol into `cs' structure. After processing the
  828. whole symbol table, only string table will be kept in memory,
  829. symbol table and debug section of xcoff will be freed. Thus
  830. we can mark symbols with names in string table as
  831. `alloced'. */
  832. {
  833. int ii;
  834. /* Swap and align the symbol into a reasonable C structure. */
  835. bfd_coff_swap_sym_in (abfd, raw_symbol, symbol);
  836. cs->c_symnum = symnum;
  837. cs->c_naux = symbol->n_numaux;
  838. if (symbol->n_zeroes)
  839. {
  840. symname_alloced = 0;
  841. /* We must use the original, unswapped, name here so the name field
  842. pointed to by cs->c_name will persist throughout xcoffread. If
  843. we use the new field, it gets overwritten for each symbol. */
  844. cs->c_name = ((struct external_syment *) raw_symbol)->e.e_name;
  845. /* If it's exactly E_SYMNMLEN characters long it isn't
  846. '\0'-terminated. */
  847. if (cs->c_name[E_SYMNMLEN - 1] != '\0')
  848. {
  849. char *p;
  850. p = (char *) obstack_alloc (&objfile->objfile_obstack,
  851. E_SYMNMLEN + 1);
  852. strncpy (p, cs->c_name, E_SYMNMLEN);
  853. p[E_SYMNMLEN] = '\0';
  854. cs->c_name = p;
  855. symname_alloced = 1;
  856. }
  857. }
  858. else if (symbol->n_sclass & 0x80)
  859. {
  860. cs->c_name = debugsec + symbol->n_offset;
  861. symname_alloced = 0;
  862. }
  863. else
  864. {
  865. /* in string table */
  866. cs->c_name = strtbl + (int) symbol->n_offset;
  867. symname_alloced = 1;
  868. }
  869. cs->c_value = symbol->n_value;
  870. cs->c_sclass = symbol->n_sclass;
  871. cs->c_secnum = symbol->n_scnum;
  872. cs->c_type = (unsigned) symbol->n_type;
  873. raw_symbol += local_symesz;
  874. ++symnum;
  875. /* Save addr of first aux entry. */
  876. raw_auxptr = raw_symbol;
  877. /* Skip all the auxents associated with this symbol. */
  878. for (ii = symbol->n_numaux; ii; --ii)
  879. {
  880. raw_symbol += coff_data (abfd)->local_auxesz;
  881. ++symnum;
  882. }
  883. }
  884. /* if symbol name starts with ".$" or "$", ignore it. */
  885. if (cs->c_name[0] == '$'
  886. || (cs->c_name[1] == '$' && cs->c_name[0] == '.'))
  887. continue;
  888. if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
  889. {
  890. if (get_last_source_file ())
  891. {
  892. pst->compunit_symtab = end_compunit_symtab
  893. (cur_src_end_addr, SECT_OFF_TEXT (objfile));
  894. end_stabs ();
  895. }
  896. start_stabs ();
  897. start_compunit_symtab (objfile, "_globals_", NULL,
  898. 0, pst_symtab_language);
  899. record_debugformat (debugfmt);
  900. cur_src_end_addr = first_object_file_end;
  901. /* Done with all files, everything from here on is globals. */
  902. }
  903. if (cs->c_sclass == C_EXT || cs->c_sclass == C_HIDEXT ||
  904. cs->c_sclass == C_WEAKEXT)
  905. {
  906. /* Dealing with a symbol with a csect entry. */
  907. #define CSECT(PP) ((PP)->x_csect)
  908. #define CSECT_LEN(PP) (CSECT(PP).x_scnlen.l)
  909. #define CSECT_ALIGN(PP) (SMTYP_ALIGN(CSECT(PP).x_smtyp))
  910. #define CSECT_SMTYP(PP) (SMTYP_SMTYP(CSECT(PP).x_smtyp))
  911. #define CSECT_SCLAS(PP) (CSECT(PP).x_smclas)
  912. /* Convert the auxent to something we can access.
  913. XCOFF can have more than one auxiliary entries.
  914. Actual functions will have two auxiliary entries, one to have the
  915. function size and other to have the smtype/smclass (LD/PR).
  916. c_type value of main symbol table will be set only in case of
  917. C_EXT/C_HIDEEXT/C_WEAKEXT storage class symbols.
  918. Bit 10 of type is set if symbol is a function, ie the value is set
  919. to 32(0x20). So we need to read the first function auxiliary entry
  920. which contains the size. */
  921. if (cs->c_naux > 1 && ISFCN (cs->c_type))
  922. {
  923. /* a function entry point. */
  924. fcn_start_addr = cs->c_value;
  925. /* save the function header info, which will be used
  926. when `.bf' is seen. */
  927. fcn_cs_saved = *cs;
  928. /* Convert the auxent to something we can access. */
  929. bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
  930. 0, cs->c_naux, &fcn_aux_saved);
  931. continue;
  932. }
  933. /* Read the csect auxiliary header, which is always the last by
  934. convention. */
  935. bfd_coff_swap_aux_in (abfd,
  936. raw_auxptr
  937. + ((coff_data (abfd)->local_symesz)
  938. * (cs->c_naux - 1)),
  939. cs->c_type, cs->c_sclass,
  940. cs->c_naux - 1, cs->c_naux,
  941. &main_aux);
  942. switch (CSECT_SMTYP (&main_aux))
  943. {
  944. case XTY_ER:
  945. /* Ignore all external references. */
  946. continue;
  947. case XTY_SD:
  948. /* A section description. */
  949. {
  950. switch (CSECT_SCLAS (&main_aux))
  951. {
  952. case XMC_PR:
  953. {
  954. /* A program csect is seen. We have to allocate one
  955. symbol table for each program csect. Normally gdb
  956. prefers one symtab for each source file. In case
  957. of AIX, one source file might include more than one
  958. [PR] csect, and they don't have to be adjacent in
  959. terms of the space they occupy in memory. Thus, one
  960. single source file might get fragmented in the
  961. memory and gdb's file start and end address
  962. approach does not work! GCC (and I think xlc) seem
  963. to put all the code in the unnamed program csect. */
  964. if (last_csect_name)
  965. {
  966. complete_symtab (filestring, file_start_addr);
  967. cur_src_end_addr = file_end_addr;
  968. end_compunit_symtab (file_end_addr,
  969. SECT_OFF_TEXT (objfile));
  970. end_stabs ();
  971. start_stabs ();
  972. /* Give all csects for this source file the same
  973. name. */
  974. start_compunit_symtab (objfile, filestring, NULL,
  975. 0, pst_symtab_language);
  976. record_debugformat (debugfmt);
  977. }
  978. /* If this is the very first csect seen,
  979. basically `__start'. */
  980. if (just_started)
  981. {
  982. first_object_file_end
  983. = cs->c_value + CSECT_LEN (&main_aux);
  984. just_started = 0;
  985. }
  986. file_start_addr =
  987. cs->c_value + objfile->text_section_offset ();
  988. file_end_addr = file_start_addr + CSECT_LEN (&main_aux);
  989. if (cs->c_name && (cs->c_name[0] == '.' || cs->c_name[0] == '@'))
  990. last_csect_name = cs->c_name;
  991. }
  992. continue;
  993. /* All other symbols are put into the minimal symbol
  994. table only. */
  995. case XMC_RW:
  996. continue;
  997. case XMC_TC0:
  998. continue;
  999. case XMC_TC:
  1000. continue;
  1001. default:
  1002. /* Ignore the symbol. */
  1003. continue;
  1004. }
  1005. }
  1006. break;
  1007. case XTY_LD:
  1008. switch (CSECT_SCLAS (&main_aux))
  1009. {
  1010. /* We never really come to this part as this case has been
  1011. handled in ISFCN check above.
  1012. This and other cases of XTY_LD are kept just for
  1013. reference. */
  1014. case XMC_PR:
  1015. continue;
  1016. case XMC_GL:
  1017. /* shared library function trampoline code entry point. */
  1018. continue;
  1019. case XMC_DS:
  1020. /* The symbols often have the same names as debug symbols for
  1021. functions, and confuse lookup_symbol. */
  1022. continue;
  1023. default:
  1024. /* xlc puts each variable in a separate csect, so we get
  1025. an XTY_SD for each variable. But gcc puts several
  1026. variables in a csect, so that each variable only gets
  1027. an XTY_LD. This will typically be XMC_RW; I suspect
  1028. XMC_RO and XMC_BS might be possible too.
  1029. These variables are put in the minimal symbol table
  1030. only. */
  1031. continue;
  1032. }
  1033. break;
  1034. case XTY_CM:
  1035. /* Common symbols are put into the minimal symbol table only. */
  1036. continue;
  1037. default:
  1038. break;
  1039. }
  1040. }
  1041. switch (cs->c_sclass)
  1042. {
  1043. case C_FILE:
  1044. /* c_value field contains symnum of next .file entry in table
  1045. or symnum of first global after last .file. */
  1046. next_file_symnum = cs->c_value;
  1047. /* Complete symbol table for last object file containing
  1048. debugging information. */
  1049. /* Whether or not there was a csect in the previous file, we
  1050. have to call `end_stabs' and `start_stabs' to reset
  1051. type_vector, line_vector, etc. structures. */
  1052. complete_symtab (filestring, file_start_addr);
  1053. cur_src_end_addr = file_end_addr;
  1054. end_compunit_symtab (file_end_addr, SECT_OFF_TEXT (objfile));
  1055. end_stabs ();
  1056. /* XCOFF, according to the AIX 3.2 documentation, puts the
  1057. filename in cs->c_name. But xlc 1.3.0.2 has decided to
  1058. do things the standard COFF way and put it in the auxent.
  1059. We use the auxent if the symbol is ".file" and an auxent
  1060. exists, otherwise use the symbol itself. Simple
  1061. enough. */
  1062. if (!strcmp (cs->c_name, ".file") && cs->c_naux > 0)
  1063. {
  1064. bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
  1065. 0, cs->c_naux, &main_aux);
  1066. filestring = coff_getfilename (&main_aux, objfile);
  1067. }
  1068. else
  1069. filestring = cs->c_name;
  1070. start_stabs ();
  1071. start_compunit_symtab (objfile, filestring, NULL, 0,
  1072. pst_symtab_language);
  1073. record_debugformat (debugfmt);
  1074. last_csect_name = 0;
  1075. /* reset file start and end addresses. A compilation unit
  1076. with no text (only data) should have zero file
  1077. boundaries. */
  1078. file_start_addr = file_end_addr = 0;
  1079. break;
  1080. case C_FUN:
  1081. fcn_stab_saved = *cs;
  1082. break;
  1083. case C_FCN:
  1084. if (strcmp (cs->c_name, ".bf") == 0)
  1085. {
  1086. CORE_ADDR off = objfile->text_section_offset ();
  1087. bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
  1088. 0, cs->c_naux, &main_aux);
  1089. within_function = 1;
  1090. newobj = push_context (0, fcn_start_addr + off);
  1091. newobj->name = define_symbol
  1092. (fcn_cs_saved.c_value + off,
  1093. fcn_stab_saved.c_name, 0, 0, objfile);
  1094. if (newobj->name != NULL)
  1095. newobj->name->set_section_index (SECT_OFF_TEXT (objfile));
  1096. }
  1097. else if (strcmp (cs->c_name, ".ef") == 0)
  1098. {
  1099. bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
  1100. 0, cs->c_naux, &main_aux);
  1101. /* The value of .ef is the address of epilogue code;
  1102. not useful for gdb. */
  1103. /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
  1104. contains number of lines to '}' */
  1105. if (outermost_context_p ())
  1106. { /* We attempted to pop an empty context stack. */
  1107. ef_complaint (cs->c_symnum);
  1108. within_function = 0;
  1109. break;
  1110. }
  1111. struct context_stack cstk = pop_context ();
  1112. /* Stack must be empty now. */
  1113. if (!outermost_context_p ())
  1114. {
  1115. ef_complaint (cs->c_symnum);
  1116. within_function = 0;
  1117. break;
  1118. }
  1119. finish_block (cstk.name, cstk.old_blocks,
  1120. NULL, cstk.start_addr,
  1121. (fcn_cs_saved.c_value
  1122. + fcn_aux_saved.x_sym.x_misc.x_fsize
  1123. + objfile->text_section_offset ()));
  1124. within_function = 0;
  1125. }
  1126. break;
  1127. case C_BSTAT:
  1128. /* Begin static block. */
  1129. {
  1130. struct internal_syment static_symbol;
  1131. read_symbol (&static_symbol, cs->c_value);
  1132. static_block_base = static_symbol.n_value;
  1133. static_block_section =
  1134. secnum_to_section (static_symbol.n_scnum, objfile);
  1135. }
  1136. break;
  1137. case C_ESTAT:
  1138. /* End of static block. */
  1139. static_block_base = 0;
  1140. static_block_section = -1;
  1141. break;
  1142. case C_ARG:
  1143. case C_REGPARM:
  1144. case C_REG:
  1145. case C_TPDEF:
  1146. case C_STRTAG:
  1147. case C_UNTAG:
  1148. case C_ENTAG:
  1149. {
  1150. complaint (_("Unrecognized storage class %d."),
  1151. cs->c_sclass);
  1152. }
  1153. break;
  1154. case C_LABEL:
  1155. case C_NULL:
  1156. /* Ignore these. */
  1157. break;
  1158. case C_HIDEXT:
  1159. case C_STAT:
  1160. break;
  1161. case C_BINCL:
  1162. /* beginning of include file */
  1163. /* In xlc output, C_BINCL/C_EINCL pair doesn't show up in sorted
  1164. order. Thus, when wee see them, we might not know enough info
  1165. to process them. Thus, we'll be saving them into a table
  1166. (inclTable) and postpone their processing. */
  1167. record_include_begin (cs);
  1168. break;
  1169. case C_EINCL:
  1170. /* End of include file. */
  1171. /* See the comment after case C_BINCL. */
  1172. record_include_end (cs);
  1173. break;
  1174. case C_BLOCK:
  1175. if (strcmp (cs->c_name, ".bb") == 0)
  1176. {
  1177. depth++;
  1178. newobj = push_context (depth,
  1179. (cs->c_value
  1180. + objfile->text_section_offset ()));
  1181. }
  1182. else if (strcmp (cs->c_name, ".eb") == 0)
  1183. {
  1184. if (outermost_context_p ())
  1185. { /* We attempted to pop an empty context stack. */
  1186. eb_complaint (cs->c_symnum);
  1187. break;
  1188. }
  1189. struct context_stack cstk = pop_context ();
  1190. if (depth-- != cstk.depth)
  1191. {
  1192. eb_complaint (cs->c_symnum);
  1193. break;
  1194. }
  1195. if (*get_local_symbols () && !outermost_context_p ())
  1196. {
  1197. /* Make a block for the local symbols within. */
  1198. finish_block (cstk.name,
  1199. cstk.old_blocks, NULL,
  1200. cstk.start_addr,
  1201. (cs->c_value
  1202. + objfile->text_section_offset ()));
  1203. }
  1204. *get_local_symbols () = cstk.locals;
  1205. }
  1206. break;
  1207. default:
  1208. process_xcoff_symbol (cs, objfile);
  1209. break;
  1210. }
  1211. }
  1212. if (get_last_source_file ())
  1213. {
  1214. struct compunit_symtab *cust;
  1215. complete_symtab (filestring, file_start_addr);
  1216. cur_src_end_addr = file_end_addr;
  1217. cust = end_compunit_symtab (file_end_addr, SECT_OFF_TEXT (objfile));
  1218. /* When reading symbols for the last C_FILE of the objfile, try
  1219. to make sure that we set pst->compunit_symtab to the symtab for the
  1220. file, not to the _globals_ symtab. I'm not sure whether this
  1221. actually works right or when/if it comes up. */
  1222. if (pst->compunit_symtab == NULL)
  1223. pst->compunit_symtab = cust;
  1224. end_stabs ();
  1225. }
  1226. }
  1227. #define SYMNAME_ALLOC(NAME, ALLOCED) \
  1228. ((ALLOCED) ? (NAME) : obstack_strdup (&objfile->objfile_obstack, \
  1229. (NAME)))
  1230. /* process one xcoff symbol. */
  1231. static struct symbol *
  1232. process_xcoff_symbol (struct coff_symbol *cs, struct objfile *objfile)
  1233. {
  1234. struct symbol onesymbol;
  1235. struct symbol *sym = &onesymbol;
  1236. struct symbol *sym2 = NULL;
  1237. char *name, *pp;
  1238. int sec;
  1239. CORE_ADDR off;
  1240. if (cs->c_secnum < 0)
  1241. {
  1242. /* The value is a register number, offset within a frame, etc.,
  1243. and does not get relocated. */
  1244. off = 0;
  1245. sec = -1;
  1246. }
  1247. else
  1248. {
  1249. sec = secnum_to_section (cs->c_secnum, objfile);
  1250. off = objfile->section_offsets[sec];
  1251. }
  1252. name = cs->c_name;
  1253. if (name[0] == '.')
  1254. ++name;
  1255. /* default assumptions */
  1256. SET_SYMBOL_VALUE_ADDRESS (sym, cs->c_value + off);
  1257. sym->set_domain (VAR_DOMAIN);
  1258. sym->set_section_index (secnum_to_section (cs->c_secnum, objfile));
  1259. if (ISFCN (cs->c_type))
  1260. {
  1261. /* At this point, we don't know the type of the function. This
  1262. will be patched with the type from its stab entry later on in
  1263. patch_block_stabs (), unless the file was compiled without -g. */
  1264. sym->set_linkage_name (SYMNAME_ALLOC (name, symname_alloced));
  1265. sym->set_type (objfile_type (objfile)->nodebug_text_symbol);
  1266. sym->set_aclass_index (LOC_BLOCK);
  1267. sym2 = new (&objfile->objfile_obstack) symbol (*sym);
  1268. if (cs->c_sclass == C_EXT || C_WEAKEXT)
  1269. add_symbol_to_list (sym2, get_global_symbols ());
  1270. else if (cs->c_sclass == C_HIDEXT || cs->c_sclass == C_STAT)
  1271. add_symbol_to_list (sym2, get_file_symbols ());
  1272. }
  1273. else
  1274. {
  1275. /* In case we can't figure out the type, provide default. */
  1276. sym->set_type (objfile_type (objfile)->nodebug_data_symbol);
  1277. switch (cs->c_sclass)
  1278. {
  1279. #if 0
  1280. /* The values of functions and global symbols are now resolved
  1281. via the global_sym_chain in stabsread.c. */
  1282. case C_FUN:
  1283. if (fcn_cs_saved.c_sclass == C_EXT)
  1284. add_stab_to_list (name, &global_stabs);
  1285. else
  1286. add_stab_to_list (name, &file_stabs);
  1287. break;
  1288. case C_GSYM:
  1289. add_stab_to_list (name, &global_stabs);
  1290. break;
  1291. #endif
  1292. case C_BCOMM:
  1293. common_block_start (cs->c_name, objfile);
  1294. break;
  1295. case C_ECOMM:
  1296. common_block_end (objfile);
  1297. break;
  1298. default:
  1299. complaint (_("Unexpected storage class: %d"),
  1300. cs->c_sclass);
  1301. /* FALLTHROUGH */
  1302. case C_DECL:
  1303. case C_PSYM:
  1304. case C_RPSYM:
  1305. case C_ECOML:
  1306. case C_LSYM:
  1307. case C_RSYM:
  1308. case C_GSYM:
  1309. {
  1310. sym = define_symbol (cs->c_value + off, cs->c_name, 0, 0, objfile);
  1311. if (sym != NULL)
  1312. {
  1313. sym->set_section_index (sec);
  1314. }
  1315. return sym;
  1316. }
  1317. case C_STSYM:
  1318. /* For xlc (not GCC), the 'V' symbol descriptor is used for
  1319. all statics and we need to distinguish file-scope versus
  1320. function-scope using within_function. We do this by
  1321. changing the string we pass to define_symbol to use 'S'
  1322. where we need to, which is not necessarily super-clean,
  1323. but seems workable enough. */
  1324. if (*name == ':')
  1325. return NULL;
  1326. pp = strchr (name, ':');
  1327. if (pp == NULL)
  1328. return NULL;
  1329. ++pp;
  1330. if (*pp == 'V' && !within_function)
  1331. *pp = 'S';
  1332. sym = define_symbol ((cs->c_value
  1333. + objfile->section_offsets[static_block_section]),
  1334. cs->c_name, 0, 0, objfile);
  1335. if (sym != NULL)
  1336. {
  1337. SET_SYMBOL_VALUE_ADDRESS (sym,
  1338. SYMBOL_VALUE_ADDRESS (sym)
  1339. + static_block_base);
  1340. sym->set_section_index (static_block_section);
  1341. }
  1342. return sym;
  1343. }
  1344. }
  1345. return sym2;
  1346. }
  1347. /* Extract the file name from the aux entry of a C_FILE symbol.
  1348. Result is in static storage and is only good for temporary use. */
  1349. static char *
  1350. coff_getfilename (union internal_auxent *aux_entry, struct objfile *objfile)
  1351. {
  1352. static char buffer[BUFSIZ];
  1353. if (aux_entry->x_file.x_n.x_n.x_zeroes == 0)
  1354. strcpy (buffer, (XCOFF_DATA (objfile)->strtbl
  1355. + aux_entry->x_file.x_n.x_n.x_offset));
  1356. else
  1357. {
  1358. strncpy (buffer, aux_entry->x_file.x_n.x_fname, FILNMLEN);
  1359. buffer[FILNMLEN] = '\0';
  1360. }
  1361. return (buffer);
  1362. }
  1363. /* Set *SYMBOL to symbol number symno in symtbl. */
  1364. static void
  1365. read_symbol (struct internal_syment *symbol, int symno)
  1366. {
  1367. struct xcoff_symfile_info *xcoff = XCOFF_DATA (this_symtab_objfile);
  1368. int nsyms = xcoff->symtbl_num_syms;
  1369. char *stbl = xcoff->symtbl;
  1370. if (symno < 0 || symno >= nsyms)
  1371. {
  1372. complaint (_("Invalid symbol offset"));
  1373. symbol->n_value = 0;
  1374. symbol->n_scnum = -1;
  1375. return;
  1376. }
  1377. bfd_coff_swap_sym_in (this_symtab_objfile->obfd,
  1378. stbl + (symno * local_symesz),
  1379. symbol);
  1380. }
  1381. /* Get value corresponding to symbol number symno in symtbl. */
  1382. static CORE_ADDR
  1383. read_symbol_nvalue (int symno)
  1384. {
  1385. struct internal_syment symbol[1];
  1386. read_symbol (symbol, symno);
  1387. return symbol->n_value;
  1388. }
  1389. /* Find the address of the function corresponding to symno, where
  1390. symno is the symbol pointed to by the linetable. */
  1391. static int
  1392. read_symbol_lineno (int symno)
  1393. {
  1394. struct objfile *objfile = this_symtab_objfile;
  1395. int xcoff64 = bfd_xcoff_is_xcoff64 (objfile->obfd);
  1396. struct xcoff_symfile_info *info = XCOFF_DATA (objfile);
  1397. int nsyms = info->symtbl_num_syms;
  1398. char *stbl = info->symtbl;
  1399. char *strtbl = info->strtbl;
  1400. struct internal_syment symbol[1];
  1401. union internal_auxent main_aux[1];
  1402. if (symno < 0)
  1403. {
  1404. bf_notfound_complaint ();
  1405. return 0;
  1406. }
  1407. /* Note that just searching for a short distance (e.g. 50 symbols)
  1408. is not enough, at least in the following case.
  1409. .extern foo
  1410. [many .stabx entries]
  1411. [a few functions, referring to foo]
  1412. .globl foo
  1413. .bf
  1414. What happens here is that the assembler moves the .stabx entries
  1415. to right before the ".bf" for foo, but the symbol for "foo" is before
  1416. all the stabx entries. See PR gdb/2222. */
  1417. /* Maintaining a table of .bf entries might be preferable to this search.
  1418. If I understand things correctly it would need to be done only for
  1419. the duration of a single psymtab to symtab conversion. */
  1420. while (symno < nsyms)
  1421. {
  1422. bfd_coff_swap_sym_in (symfile_bfd,
  1423. stbl + (symno * local_symesz), symbol);
  1424. if (symbol->n_sclass == C_FCN)
  1425. {
  1426. char *name = xcoff64 ? strtbl + symbol->n_offset : symbol->n_name;
  1427. if (strcmp (name, ".bf") == 0)
  1428. goto gotit;
  1429. }
  1430. symno += symbol->n_numaux + 1;
  1431. }
  1432. bf_notfound_complaint ();
  1433. return 0;
  1434. gotit:
  1435. /* Take aux entry and return its lineno. */
  1436. symno++;
  1437. bfd_coff_swap_aux_in (objfile->obfd, stbl + symno * local_symesz,
  1438. symbol->n_type, symbol->n_sclass,
  1439. 0, symbol->n_numaux, main_aux);
  1440. return main_aux->x_sym.x_misc.x_lnsz.x_lnno;
  1441. }
  1442. /* Support for line number handling. */
  1443. /* This function is called for every section; it finds the outer limits
  1444. * of the line table (minimum and maximum file offset) so that the
  1445. * mainline code can read the whole thing for efficiency.
  1446. */
  1447. static void
  1448. find_linenos (struct bfd *abfd, struct bfd_section *asect, void *vpinfo)
  1449. {
  1450. struct xcoff_symfile_info *info;
  1451. int size, count;
  1452. file_ptr offset, maxoff;
  1453. count = asect->lineno_count;
  1454. if (strcmp (asect->name, ".text") != 0 || count == 0)
  1455. return;
  1456. size = count * coff_data (abfd)->local_linesz;
  1457. info = (struct xcoff_symfile_info *) vpinfo;
  1458. offset = asect->line_filepos;
  1459. maxoff = offset + size;
  1460. if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
  1461. info->min_lineno_offset = offset;
  1462. if (maxoff > info->max_lineno_offset)
  1463. info->max_lineno_offset = maxoff;
  1464. }
  1465. static void
  1466. xcoff_expand_psymtab (legacy_psymtab *pst, struct objfile *objfile)
  1467. {
  1468. gdb_assert (!pst->readin);
  1469. /* Read in all partial symtabs on which this one is dependent. */
  1470. pst->expand_dependencies (objfile);
  1471. if (((struct symloc *) pst->read_symtab_private)->numsyms != 0)
  1472. {
  1473. /* Init stuff necessary for reading in symbols. */
  1474. stabsread_init ();
  1475. scoped_free_pendings free_pending;
  1476. read_xcoff_symtab (objfile, pst);
  1477. }
  1478. pst->readin = true;
  1479. }
  1480. /* Read in all of the symbols for a given psymtab for real.
  1481. Be verbose about it if the user wants that. SELF is not NULL. */
  1482. static void
  1483. xcoff_read_symtab (legacy_psymtab *self, struct objfile *objfile)
  1484. {
  1485. gdb_assert (!self->readin);
  1486. if (((struct symloc *) self->read_symtab_private)->numsyms != 0
  1487. || self->number_of_dependencies)
  1488. {
  1489. next_symbol_text_func = xcoff_next_symbol_text;
  1490. self->expand_psymtab (objfile);
  1491. /* Match with global symbols. This only needs to be done once,
  1492. after all of the symtabs and dependencies have been read in. */
  1493. scan_file_globals (objfile);
  1494. }
  1495. }
  1496. static void
  1497. xcoff_new_init (struct objfile *objfile)
  1498. {
  1499. stabsread_new_init ();
  1500. }
  1501. /* Do initialization in preparation for reading symbols from OBJFILE.
  1502. We will only be called if this is an XCOFF or XCOFF-like file.
  1503. BFD handles figuring out the format of the file, and code in symfile.c
  1504. uses BFD's determination to vector to us. */
  1505. static void
  1506. xcoff_symfile_init (struct objfile *objfile)
  1507. {
  1508. /* Allocate struct to keep track of the symfile. */
  1509. xcoff_objfile_data_key.emplace (objfile);
  1510. /* XCOFF objects may be reordered, so set OBJF_REORDERED. If we
  1511. find this causes a significant slowdown in gdb then we could
  1512. set it in the debug symbol readers only when necessary. */
  1513. objfile->flags |= OBJF_REORDERED;
  1514. }
  1515. /* Perform any local cleanups required when we are done with a particular
  1516. objfile. I.E, we are in the process of discarding all symbol information
  1517. for an objfile, freeing up all memory held for it, and unlinking the
  1518. objfile struct from the global list of known objfiles. */
  1519. static void
  1520. xcoff_symfile_finish (struct objfile *objfile)
  1521. {
  1522. /* Start with a fresh include table for the next objfile. */
  1523. if (inclTable)
  1524. {
  1525. xfree (inclTable);
  1526. inclTable = NULL;
  1527. }
  1528. inclIndx = inclLength = inclDepth = 0;
  1529. }
  1530. static void
  1531. init_stringtab (bfd *abfd, file_ptr offset, struct objfile *objfile)
  1532. {
  1533. long length;
  1534. int val;
  1535. unsigned char lengthbuf[4];
  1536. char *strtbl;
  1537. struct xcoff_symfile_info *xcoff = XCOFF_DATA (objfile);
  1538. xcoff->strtbl = NULL;
  1539. if (bfd_seek (abfd, offset, SEEK_SET) < 0)
  1540. error (_("cannot seek to string table in %s: %s"),
  1541. bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
  1542. val = bfd_bread ((char *) lengthbuf, sizeof lengthbuf, abfd);
  1543. length = bfd_h_get_32 (abfd, lengthbuf);
  1544. /* If no string table is needed, then the file may end immediately
  1545. after the symbols. Just return with `strtbl' set to NULL. */
  1546. if (val != sizeof lengthbuf || length < sizeof lengthbuf)
  1547. return;
  1548. /* Allocate string table from objfile_obstack. We will need this table
  1549. as long as we have its symbol table around. */
  1550. strtbl = (char *) obstack_alloc (&objfile->objfile_obstack, length);
  1551. xcoff->strtbl = strtbl;
  1552. /* Copy length buffer, the first byte is usually zero and is
  1553. used for stabs with a name length of zero. */
  1554. memcpy (strtbl, lengthbuf, sizeof lengthbuf);
  1555. if (length == sizeof lengthbuf)
  1556. return;
  1557. val = bfd_bread (strtbl + sizeof lengthbuf, length - sizeof lengthbuf, abfd);
  1558. if (val != length - sizeof lengthbuf)
  1559. error (_("cannot read string table from %s: %s"),
  1560. bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
  1561. if (strtbl[length - 1] != '\0')
  1562. error (_("bad symbol file: string table "
  1563. "does not end with null character"));
  1564. return;
  1565. }
  1566. /* If we have not yet seen a function for this psymtab, this is 0. If we
  1567. have seen one, it is the offset in the line numbers of the line numbers
  1568. for the psymtab. */
  1569. static unsigned int first_fun_line_offset;
  1570. /* Allocate and partially fill a partial symtab. It will be
  1571. completely filled at the end of the symbol list.
  1572. SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
  1573. is the address relative to which its symbols are (incremental) or 0
  1574. (normal). */
  1575. static legacy_psymtab *
  1576. xcoff_start_psymtab (psymtab_storage *partial_symtabs,
  1577. struct objfile *objfile,
  1578. const char *filename, int first_symnum)
  1579. {
  1580. /* We fill in textlow later. */
  1581. legacy_psymtab *result = new legacy_psymtab (filename, partial_symtabs,
  1582. objfile->per_bfd, 0);
  1583. result->read_symtab_private =
  1584. XOBNEW (&objfile->objfile_obstack, struct symloc);
  1585. ((struct symloc *) result->read_symtab_private)->first_symnum = first_symnum;
  1586. result->legacy_read_symtab = xcoff_read_symtab;
  1587. result->legacy_expand_psymtab = xcoff_expand_psymtab;
  1588. /* Deduce the source language from the filename for this psymtab. */
  1589. psymtab_language = deduce_language_from_filename (filename);
  1590. return result;
  1591. }
  1592. /* Close off the current usage of PST.
  1593. Returns PST, or NULL if the partial symtab was empty and thrown away.
  1594. CAPPING_SYMBOL_NUMBER is the end of pst (exclusive).
  1595. INCLUDE_LIST, NUM_INCLUDES, DEPENDENCY_LIST, and NUMBER_DEPENDENCIES
  1596. are the information for includes and dependencies. */
  1597. static legacy_psymtab *
  1598. xcoff_end_psymtab (struct objfile *objfile, psymtab_storage *partial_symtabs,
  1599. legacy_psymtab *pst,
  1600. const char **include_list, int num_includes,
  1601. int capping_symbol_number,
  1602. legacy_psymtab **dependency_list,
  1603. int number_dependencies, int textlow_not_set)
  1604. {
  1605. int i;
  1606. if (capping_symbol_number != -1)
  1607. ((struct symloc *) pst->read_symtab_private)->numsyms =
  1608. capping_symbol_number
  1609. - ((struct symloc *) pst->read_symtab_private)->first_symnum;
  1610. ((struct symloc *) pst->read_symtab_private)->lineno_off =
  1611. first_fun_line_offset;
  1612. first_fun_line_offset = 0;
  1613. pst->end ();
  1614. pst->number_of_dependencies = number_dependencies;
  1615. if (number_dependencies)
  1616. {
  1617. pst->dependencies
  1618. = partial_symtabs->allocate_dependencies (number_dependencies);
  1619. memcpy (pst->dependencies, dependency_list,
  1620. number_dependencies * sizeof (legacy_psymtab *));
  1621. }
  1622. else
  1623. pst->dependencies = 0;
  1624. for (i = 0; i < num_includes; i++)
  1625. {
  1626. legacy_psymtab *subpst =
  1627. new legacy_psymtab (include_list[i], partial_symtabs, objfile->per_bfd);
  1628. subpst->read_symtab_private = XOBNEW (&objfile->objfile_obstack, symloc);
  1629. ((struct symloc *) subpst->read_symtab_private)->first_symnum = 0;
  1630. ((struct symloc *) subpst->read_symtab_private)->numsyms = 0;
  1631. /* We could save slight bits of space by only making one of these,
  1632. shared by the entire set of include files. FIXME-someday. */
  1633. subpst->dependencies =
  1634. partial_symtabs->allocate_dependencies (1);
  1635. subpst->dependencies[0] = pst;
  1636. subpst->number_of_dependencies = 1;
  1637. subpst->legacy_read_symtab = pst->legacy_read_symtab;
  1638. subpst->legacy_expand_psymtab = pst->legacy_expand_psymtab;
  1639. }
  1640. if (num_includes == 0
  1641. && number_dependencies == 0
  1642. && pst->empty ())
  1643. {
  1644. /* Throw away this psymtab, it's empty. */
  1645. /* Empty psymtabs happen as a result of header files which don't have
  1646. any symbols in them. There can be a lot of them. */
  1647. partial_symtabs->discard_psymtab (pst);
  1648. /* Indicate that psymtab was thrown away. */
  1649. pst = NULL;
  1650. }
  1651. return pst;
  1652. }
  1653. /* Swap raw symbol at *RAW and put the name in *NAME, the symbol in
  1654. *SYMBOL, the first auxent in *AUX. Advance *RAW and *SYMNUMP over
  1655. the symbol and its auxents. */
  1656. static void
  1657. swap_sym (struct internal_syment *symbol, union internal_auxent *aux,
  1658. const char **name, char **raw, unsigned int *symnump,
  1659. struct objfile *objfile)
  1660. {
  1661. bfd_coff_swap_sym_in (objfile->obfd, *raw, symbol);
  1662. if (symbol->n_zeroes)
  1663. {
  1664. /* If it's exactly E_SYMNMLEN characters long it isn't
  1665. '\0'-terminated. */
  1666. if (symbol->n_name[E_SYMNMLEN - 1] != '\0')
  1667. {
  1668. /* FIXME: wastes memory for symbols which we don't end up putting
  1669. into the minimal symbols. */
  1670. char *p;
  1671. p = (char *) obstack_alloc (&objfile->objfile_obstack,
  1672. E_SYMNMLEN + 1);
  1673. strncpy (p, symbol->n_name, E_SYMNMLEN);
  1674. p[E_SYMNMLEN] = '\0';
  1675. *name = p;
  1676. }
  1677. else
  1678. /* Point to the unswapped name as that persists as long as the
  1679. objfile does. */
  1680. *name = ((struct external_syment *) *raw)->e.e_name;
  1681. }
  1682. else if (symbol->n_sclass & 0x80)
  1683. {
  1684. *name = XCOFF_DATA (objfile)->debugsec + symbol->n_offset;
  1685. }
  1686. else
  1687. {
  1688. *name = XCOFF_DATA (objfile)->strtbl + symbol->n_offset;
  1689. }
  1690. ++*symnump;
  1691. *raw += coff_data (objfile->obfd)->local_symesz;
  1692. if (symbol->n_numaux > 0)
  1693. {
  1694. bfd_coff_swap_aux_in (objfile->obfd, *raw, symbol->n_type,
  1695. symbol->n_sclass, 0, symbol->n_numaux, aux);
  1696. *symnump += symbol->n_numaux;
  1697. *raw += coff_data (objfile->obfd)->local_symesz * symbol->n_numaux;
  1698. }
  1699. }
  1700. static void
  1701. function_outside_compilation_unit_complaint (const char *arg1)
  1702. {
  1703. complaint (_("function `%s' appears to be defined "
  1704. "outside of all compilation units"),
  1705. arg1);
  1706. }
  1707. static void
  1708. scan_xcoff_symtab (minimal_symbol_reader &reader,
  1709. psymtab_storage *partial_symtabs,
  1710. struct objfile *objfile)
  1711. {
  1712. CORE_ADDR toc_offset = 0; /* toc offset value in data section. */
  1713. const char *filestring = NULL;
  1714. const char *namestring;
  1715. bfd *abfd;
  1716. asection *bfd_sect;
  1717. unsigned int nsyms;
  1718. /* Current partial symtab */
  1719. legacy_psymtab *pst;
  1720. /* List of current psymtab's include files. */
  1721. const char **psymtab_include_list;
  1722. int includes_allocated;
  1723. int includes_used;
  1724. /* Index within current psymtab dependency list. */
  1725. legacy_psymtab **dependency_list;
  1726. int dependencies_used, dependencies_allocated;
  1727. char *sraw_symbol;
  1728. struct internal_syment symbol;
  1729. union internal_auxent main_aux[5];
  1730. unsigned int ssymnum;
  1731. const char *last_csect_name = NULL; /* Last seen csect's name and value. */
  1732. CORE_ADDR last_csect_val = 0;
  1733. int last_csect_sec = 0;
  1734. int misc_func_recorded = 0; /* true if any misc. function. */
  1735. int textlow_not_set = 1;
  1736. pst = (legacy_psymtab *) 0;
  1737. includes_allocated = 30;
  1738. includes_used = 0;
  1739. psymtab_include_list = (const char **) alloca (includes_allocated *
  1740. sizeof (const char *));
  1741. dependencies_allocated = 30;
  1742. dependencies_used = 0;
  1743. dependency_list =
  1744. (legacy_psymtab **) alloca (dependencies_allocated *
  1745. sizeof (legacy_psymtab *));
  1746. set_last_source_file (NULL);
  1747. abfd = objfile->obfd;
  1748. next_symbol_text_func = xcoff_next_symbol_text;
  1749. sraw_symbol = XCOFF_DATA (objfile)->symtbl;
  1750. nsyms = XCOFF_DATA (objfile)->symtbl_num_syms;
  1751. ssymnum = 0;
  1752. while (ssymnum < nsyms)
  1753. {
  1754. int sclass;
  1755. QUIT;
  1756. bfd_coff_swap_sym_in (abfd, sraw_symbol, &symbol);
  1757. sclass = symbol.n_sclass;
  1758. switch (sclass)
  1759. {
  1760. case C_EXT:
  1761. case C_HIDEXT:
  1762. case C_WEAKEXT:
  1763. {
  1764. /* The CSECT auxent--always the last auxent. */
  1765. union internal_auxent csect_aux;
  1766. unsigned int symnum_before = ssymnum;
  1767. swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
  1768. &ssymnum, objfile);
  1769. if (symbol.n_numaux > 1)
  1770. {
  1771. bfd_coff_swap_aux_in
  1772. (objfile->obfd,
  1773. sraw_symbol - coff_data (abfd)->local_symesz,
  1774. symbol.n_type,
  1775. symbol.n_sclass,
  1776. symbol.n_numaux - 1,
  1777. symbol.n_numaux,
  1778. &csect_aux);
  1779. }
  1780. else
  1781. csect_aux = main_aux[0];
  1782. /* If symbol name starts with ".$" or "$", ignore it. */
  1783. if (namestring[0] == '$'
  1784. || (namestring[0] == '.' && namestring[1] == '$'))
  1785. break;
  1786. switch (csect_aux.x_csect.x_smtyp & 0x7)
  1787. {
  1788. case XTY_SD:
  1789. switch (csect_aux.x_csect.x_smclas)
  1790. {
  1791. case XMC_PR:
  1792. if (last_csect_name)
  1793. {
  1794. /* If no misc. function recorded in the last
  1795. seen csect, enter it as a function. This
  1796. will take care of functions like strcmp()
  1797. compiled by xlc. */
  1798. if (!misc_func_recorded)
  1799. {
  1800. record_minimal_symbol
  1801. (reader, last_csect_name, last_csect_val,
  1802. mst_text, last_csect_sec, objfile);
  1803. misc_func_recorded = 1;
  1804. }
  1805. if (pst != NULL)
  1806. {
  1807. /* We have to allocate one psymtab for
  1808. each program csect, because their text
  1809. sections need not be adjacent. */
  1810. xcoff_end_psymtab
  1811. (objfile, partial_symtabs, pst, psymtab_include_list,
  1812. includes_used, symnum_before, dependency_list,
  1813. dependencies_used, textlow_not_set);
  1814. includes_used = 0;
  1815. dependencies_used = 0;
  1816. /* Give all psymtabs for this source file the same
  1817. name. */
  1818. pst = xcoff_start_psymtab
  1819. (partial_symtabs, objfile,
  1820. filestring,
  1821. symnum_before);
  1822. }
  1823. }
  1824. /* Activate the misc_func_recorded mechanism for
  1825. compiler- and linker-generated CSECTs like ".strcmp"
  1826. and "@FIX1". */
  1827. if (namestring && (namestring[0] == '.'
  1828. || namestring[0] == '@'))
  1829. {
  1830. last_csect_name = namestring;
  1831. last_csect_val = symbol.n_value;
  1832. last_csect_sec = symbol.n_scnum;
  1833. }
  1834. if (pst != NULL)
  1835. {
  1836. CORE_ADDR highval =
  1837. symbol.n_value + csect_aux.x_csect.x_scnlen.l;
  1838. if (highval > pst->raw_text_high ())
  1839. pst->set_text_high (highval);
  1840. if (!pst->text_low_valid
  1841. || symbol.n_value < pst->raw_text_low ())
  1842. pst->set_text_low (symbol.n_value);
  1843. }
  1844. misc_func_recorded = 0;
  1845. break;
  1846. case XMC_RW:
  1847. case XMC_TD:
  1848. /* Data variables are recorded in the minimal symbol
  1849. table, except for section symbols. */
  1850. if (*namestring != '.')
  1851. record_minimal_symbol
  1852. (reader, namestring, symbol.n_value,
  1853. sclass == C_HIDEXT ? mst_file_data : mst_data,
  1854. symbol.n_scnum, objfile);
  1855. break;
  1856. case XMC_TC0:
  1857. if (toc_offset)
  1858. warning (_("More than one XMC_TC0 symbol found."));
  1859. toc_offset = symbol.n_value;
  1860. /* Make TOC offset relative to start address of
  1861. section. */
  1862. bfd_sect = secnum_to_bfd_section (symbol.n_scnum, objfile);
  1863. if (bfd_sect)
  1864. toc_offset -= bfd_section_vma (bfd_sect);
  1865. break;
  1866. case XMC_TC:
  1867. /* These symbols tell us where the TOC entry for a
  1868. variable is, not the variable itself. */
  1869. break;
  1870. default:
  1871. break;
  1872. }
  1873. break;
  1874. case XTY_LD:
  1875. switch (csect_aux.x_csect.x_smclas)
  1876. {
  1877. case XMC_PR:
  1878. /* A function entry point. */
  1879. if (first_fun_line_offset == 0 && symbol.n_numaux > 1)
  1880. first_fun_line_offset =
  1881. main_aux[0].x_sym.x_fcnary.x_fcn.x_lnnoptr;
  1882. record_minimal_symbol
  1883. (reader, namestring, symbol.n_value,
  1884. sclass == C_HIDEXT ? mst_file_text : mst_text,
  1885. symbol.n_scnum, objfile);
  1886. misc_func_recorded = 1;
  1887. break;
  1888. case XMC_GL:
  1889. /* shared library function trampoline code entry
  1890. point. */
  1891. /* record trampoline code entries as
  1892. mst_solib_trampoline symbol. When we lookup mst
  1893. symbols, we will choose mst_text over
  1894. mst_solib_trampoline. */
  1895. record_minimal_symbol
  1896. (reader, namestring, symbol.n_value,
  1897. mst_solib_trampoline, symbol.n_scnum, objfile);
  1898. misc_func_recorded = 1;
  1899. break;
  1900. case XMC_DS:
  1901. /* The symbols often have the same names as
  1902. debug symbols for functions, and confuse
  1903. lookup_symbol. */
  1904. break;
  1905. default:
  1906. /* xlc puts each variable in a separate csect,
  1907. so we get an XTY_SD for each variable. But
  1908. gcc puts several variables in a csect, so
  1909. that each variable only gets an XTY_LD. We
  1910. still need to record them. This will
  1911. typically be XMC_RW; I suspect XMC_RO and
  1912. XMC_BS might be possible too. */
  1913. if (*namestring != '.')
  1914. record_minimal_symbol
  1915. (reader, namestring, symbol.n_value,
  1916. sclass == C_HIDEXT ? mst_file_data : mst_data,
  1917. symbol.n_scnum, objfile);
  1918. break;
  1919. }
  1920. break;
  1921. case XTY_CM:
  1922. switch (csect_aux.x_csect.x_smclas)
  1923. {
  1924. case XMC_RW:
  1925. case XMC_BS:
  1926. /* Common variables are recorded in the minimal symbol
  1927. table, except for section symbols. */
  1928. if (*namestring != '.')
  1929. record_minimal_symbol
  1930. (reader, namestring, symbol.n_value,
  1931. sclass == C_HIDEXT ? mst_file_bss : mst_bss,
  1932. symbol.n_scnum, objfile);
  1933. break;
  1934. }
  1935. break;
  1936. default:
  1937. break;
  1938. }
  1939. }
  1940. break;
  1941. case C_FILE:
  1942. {
  1943. unsigned int symnum_before;
  1944. symnum_before = ssymnum;
  1945. swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
  1946. &ssymnum, objfile);
  1947. /* See if the last csect needs to be recorded. */
  1948. if (last_csect_name && !misc_func_recorded)
  1949. {
  1950. /* If no misc. function recorded in the last seen csect, enter
  1951. it as a function. This will take care of functions like
  1952. strcmp() compiled by xlc. */
  1953. record_minimal_symbol (reader, last_csect_name, last_csect_val,
  1954. mst_text, last_csect_sec, objfile);
  1955. misc_func_recorded = 1;
  1956. }
  1957. if (pst)
  1958. {
  1959. xcoff_end_psymtab (objfile, partial_symtabs,
  1960. pst, psymtab_include_list,
  1961. includes_used, symnum_before,
  1962. dependency_list, dependencies_used,
  1963. textlow_not_set);
  1964. includes_used = 0;
  1965. dependencies_used = 0;
  1966. }
  1967. first_fun_line_offset = 0;
  1968. /* XCOFF, according to the AIX 3.2 documentation, puts the
  1969. filename in cs->c_name. But xlc 1.3.0.2 has decided to
  1970. do things the standard COFF way and put it in the auxent.
  1971. We use the auxent if the symbol is ".file" and an auxent
  1972. exists, otherwise use the symbol itself. */
  1973. if (!strcmp (namestring, ".file") && symbol.n_numaux > 0)
  1974. {
  1975. filestring = coff_getfilename (&main_aux[0], objfile);
  1976. }
  1977. else
  1978. filestring = namestring;
  1979. pst = xcoff_start_psymtab (partial_symtabs, objfile,
  1980. filestring,
  1981. symnum_before);
  1982. last_csect_name = NULL;
  1983. }
  1984. break;
  1985. default:
  1986. {
  1987. complaint (_("Storage class %d not recognized during scan"),
  1988. sclass);
  1989. }
  1990. /* FALLTHROUGH */
  1991. case C_FCN:
  1992. /* C_FCN is .bf and .ef symbols. I think it is sufficient
  1993. to handle only the C_FUN and C_EXT. */
  1994. case C_BSTAT:
  1995. case C_ESTAT:
  1996. case C_ARG:
  1997. case C_REGPARM:
  1998. case C_REG:
  1999. case C_TPDEF:
  2000. case C_STRTAG:
  2001. case C_UNTAG:
  2002. case C_ENTAG:
  2003. case C_LABEL:
  2004. case C_NULL:
  2005. /* C_EINCL means we are switching back to the main file. But there
  2006. is no reason to care; the only thing we want to know about
  2007. includes is the names of all the included (.h) files. */
  2008. case C_EINCL:
  2009. case C_BLOCK:
  2010. /* I don't think C_STAT is used in xcoff; C_HIDEXT appears to be
  2011. used instead. */
  2012. case C_STAT:
  2013. /* I don't think the name of the common block (as opposed to the
  2014. variables within it) is something which is user visible
  2015. currently. */
  2016. case C_BCOMM:
  2017. case C_ECOMM:
  2018. case C_PSYM:
  2019. case C_RPSYM:
  2020. /* I think we can ignore C_LSYM; types on xcoff seem to use C_DECL
  2021. so C_LSYM would appear to be only for locals. */
  2022. case C_LSYM:
  2023. case C_AUTO:
  2024. case C_RSYM:
  2025. {
  2026. /* We probably could save a few instructions by assuming that
  2027. C_LSYM, C_PSYM, etc., never have auxents. */
  2028. int naux1 = symbol.n_numaux + 1;
  2029. ssymnum += naux1;
  2030. sraw_symbol += bfd_coff_symesz (abfd) * naux1;
  2031. }
  2032. break;
  2033. case C_BINCL:
  2034. {
  2035. /* Mark down an include file in the current psymtab. */
  2036. enum language tmp_language;
  2037. swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
  2038. &ssymnum, objfile);
  2039. tmp_language = deduce_language_from_filename (namestring);
  2040. /* Only change the psymtab's language if we've learned
  2041. something useful (eg. tmp_language is not language_unknown).
  2042. In addition, to match what start_subfile does, never change
  2043. from C++ to C. */
  2044. if (tmp_language != language_unknown
  2045. && (tmp_language != language_c
  2046. || psymtab_language != language_cplus))
  2047. psymtab_language = tmp_language;
  2048. /* In C++, one may expect the same filename to come round many
  2049. times, when code is coming alternately from the main file
  2050. and from inline functions in other files. So I check to see
  2051. if this is a file we've seen before -- either the main
  2052. source file, or a previously included file.
  2053. This seems to be a lot of time to be spending on N_SOL, but
  2054. things like "break c-exp.y:435" need to work (I
  2055. suppose the psymtab_include_list could be hashed or put
  2056. in a binary tree, if profiling shows this is a major hog). */
  2057. if (pst && strcmp (namestring, pst->filename) == 0)
  2058. continue;
  2059. {
  2060. int i;
  2061. for (i = 0; i < includes_used; i++)
  2062. if (strcmp (namestring, psymtab_include_list[i]) == 0)
  2063. {
  2064. i = -1;
  2065. break;
  2066. }
  2067. if (i == -1)
  2068. continue;
  2069. }
  2070. psymtab_include_list[includes_used++] = namestring;
  2071. if (includes_used >= includes_allocated)
  2072. {
  2073. const char **orig = psymtab_include_list;
  2074. psymtab_include_list = (const char **)
  2075. alloca ((includes_allocated *= 2) *
  2076. sizeof (const char *));
  2077. memcpy (psymtab_include_list, orig,
  2078. includes_used * sizeof (const char *));
  2079. }
  2080. continue;
  2081. }
  2082. case C_FUN:
  2083. /* The value of the C_FUN is not the address of the function (it
  2084. appears to be the address before linking), but as long as it
  2085. is smaller than the actual address, then find_pc_partial_function
  2086. will use the minimal symbols instead. I hope. */
  2087. case C_GSYM:
  2088. case C_ECOML:
  2089. case C_DECL:
  2090. case C_STSYM:
  2091. {
  2092. const char *p;
  2093. swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
  2094. &ssymnum, objfile);
  2095. p = strchr (namestring, ':');
  2096. if (!p)
  2097. continue; /* Not a debugging symbol. */
  2098. /* Main processing section for debugging symbols which
  2099. the initial read through the symbol tables needs to worry
  2100. about. If we reach this point, the symbol which we are
  2101. considering is definitely one we are interested in.
  2102. p must also contain the (valid) index into the namestring
  2103. which indicates the debugging type symbol. */
  2104. switch (p[1])
  2105. {
  2106. case 'S':
  2107. pst->add_psymbol (gdb::string_view (namestring,
  2108. p - namestring),
  2109. true, VAR_DOMAIN, LOC_STATIC,
  2110. SECT_OFF_DATA (objfile),
  2111. psymbol_placement::STATIC,
  2112. symbol.n_value,
  2113. psymtab_language,
  2114. partial_symtabs, objfile);
  2115. continue;
  2116. case 'G':
  2117. /* The addresses in these entries are reported to be
  2118. wrong. See the code that reads 'G's for symtabs. */
  2119. pst->add_psymbol (gdb::string_view (namestring,
  2120. p - namestring),
  2121. true, VAR_DOMAIN, LOC_STATIC,
  2122. SECT_OFF_DATA (objfile),
  2123. psymbol_placement::GLOBAL,
  2124. symbol.n_value,
  2125. psymtab_language,
  2126. partial_symtabs, objfile);
  2127. continue;
  2128. case 'T':
  2129. /* When a 'T' entry is defining an anonymous enum, it
  2130. may have a name which is the empty string, or a
  2131. single space. Since they're not really defining a
  2132. symbol, those shouldn't go in the partial symbol
  2133. table. We do pick up the elements of such enums at
  2134. 'check_enum:', below. */
  2135. if (p >= namestring + 2
  2136. || (p == namestring + 1
  2137. && namestring[0] != ' '))
  2138. {
  2139. pst->add_psymbol (gdb::string_view (namestring,
  2140. p - namestring),
  2141. true, STRUCT_DOMAIN, LOC_TYPEDEF, -1,
  2142. psymbol_placement::STATIC,
  2143. 0, psymtab_language,
  2144. partial_symtabs, objfile);
  2145. if (p[2] == 't')
  2146. {
  2147. /* Also a typedef with the same name. */
  2148. pst->add_psymbol (gdb::string_view (namestring,
  2149. p - namestring),
  2150. true, VAR_DOMAIN, LOC_TYPEDEF, -1,
  2151. psymbol_placement::STATIC,
  2152. 0, psymtab_language,
  2153. partial_symtabs, objfile);
  2154. p += 1;
  2155. }
  2156. }
  2157. goto check_enum;
  2158. case 't':
  2159. if (p != namestring) /* a name is there, not just :T... */
  2160. {
  2161. pst->add_psymbol (gdb::string_view (namestring,
  2162. p - namestring),
  2163. true, VAR_DOMAIN, LOC_TYPEDEF, -1,
  2164. psymbol_placement::STATIC,
  2165. 0, psymtab_language,
  2166. partial_symtabs, objfile);
  2167. }
  2168. check_enum:
  2169. /* If this is an enumerated type, we need to
  2170. add all the enum constants to the partial symbol
  2171. table. This does not cover enums without names, e.g.
  2172. "enum {a, b} c;" in C, but fortunately those are
  2173. rare. There is no way for GDB to find those from the
  2174. enum type without spending too much time on it. Thus
  2175. to solve this problem, the compiler needs to put out the
  2176. enum in a nameless type. GCC2 does this. */
  2177. /* We are looking for something of the form
  2178. <name> ":" ("t" | "T") [<number> "="] "e"
  2179. {<constant> ":" <value> ","} ";". */
  2180. /* Skip over the colon and the 't' or 'T'. */
  2181. p += 2;
  2182. /* This type may be given a number. Also, numbers can come
  2183. in pairs like (0,26). Skip over it. */
  2184. while ((*p >= '0' && *p <= '9')
  2185. || *p == '(' || *p == ',' || *p == ')'
  2186. || *p == '=')
  2187. p++;
  2188. if (*p++ == 'e')
  2189. {
  2190. /* The aix4 compiler emits extra crud before the
  2191. members. */
  2192. if (*p == '-')
  2193. {
  2194. /* Skip over the type (?). */
  2195. while (*p != ':')
  2196. p++;
  2197. /* Skip over the colon. */
  2198. p++;
  2199. }
  2200. /* We have found an enumerated type. */
  2201. /* According to comments in read_enum_type
  2202. a comma could end it instead of a semicolon.
  2203. I don't know where that happens.
  2204. Accept either. */
  2205. while (*p && *p != ';' && *p != ',')
  2206. {
  2207. const char *q;
  2208. /* Check for and handle cretinous dbx symbol name
  2209. continuation! */
  2210. if (*p == '\\' || (*p == '?' && p[1] == '\0'))
  2211. p = next_symbol_text (objfile);
  2212. /* Point to the character after the name
  2213. of the enum constant. */
  2214. for (q = p; *q && *q != ':'; q++)
  2215. ;
  2216. /* Note that the value doesn't matter for
  2217. enum constants in psymtabs, just in symtabs. */
  2218. pst->add_psymbol (gdb::string_view (p, q - p), true,
  2219. VAR_DOMAIN, LOC_CONST, -1,
  2220. psymbol_placement::STATIC,
  2221. 0, psymtab_language,
  2222. partial_symtabs, objfile);
  2223. /* Point past the name. */
  2224. p = q;
  2225. /* Skip over the value. */
  2226. while (*p && *p != ',')
  2227. p++;
  2228. /* Advance past the comma. */
  2229. if (*p)
  2230. p++;
  2231. }
  2232. }
  2233. continue;
  2234. case 'c':
  2235. /* Constant, e.g. from "const" in Pascal. */
  2236. pst->add_psymbol (gdb::string_view (namestring,
  2237. p - namestring),
  2238. true, VAR_DOMAIN, LOC_CONST, -1,
  2239. psymbol_placement::STATIC,
  2240. 0, psymtab_language,
  2241. partial_symtabs, objfile);
  2242. continue;
  2243. case 'f':
  2244. if (! pst)
  2245. {
  2246. std::string name (namestring, (p - namestring));
  2247. function_outside_compilation_unit_complaint (name.c_str ());
  2248. }
  2249. pst->add_psymbol (gdb::string_view (namestring,
  2250. p - namestring),
  2251. true, VAR_DOMAIN, LOC_BLOCK,
  2252. SECT_OFF_TEXT (objfile),
  2253. psymbol_placement::STATIC,
  2254. symbol.n_value,
  2255. psymtab_language,
  2256. partial_symtabs, objfile);
  2257. continue;
  2258. /* Global functions were ignored here, but now they
  2259. are put into the global psymtab like one would expect.
  2260. They're also in the minimal symbol table. */
  2261. case 'F':
  2262. if (! pst)
  2263. {
  2264. std::string name (namestring, (p - namestring));
  2265. function_outside_compilation_unit_complaint (name.c_str ());
  2266. }
  2267. /* We need only the minimal symbols for these
  2268. loader-generated definitions. Keeping the global
  2269. symbols leads to "in psymbols but not in symbols"
  2270. errors. */
  2271. if (startswith (namestring, "@FIX"))
  2272. continue;
  2273. pst->add_psymbol (gdb::string_view (namestring,
  2274. p - namestring),
  2275. true, VAR_DOMAIN, LOC_BLOCK,
  2276. SECT_OFF_TEXT (objfile),
  2277. psymbol_placement::GLOBAL,
  2278. symbol.n_value,
  2279. psymtab_language,
  2280. partial_symtabs, objfile);
  2281. continue;
  2282. /* Two things show up here (hopefully); static symbols of
  2283. local scope (static used inside braces) or extensions
  2284. of structure symbols. We can ignore both. */
  2285. case 'V':
  2286. case '(':
  2287. case '0':
  2288. case '1':
  2289. case '2':
  2290. case '3':
  2291. case '4':
  2292. case '5':
  2293. case '6':
  2294. case '7':
  2295. case '8':
  2296. case '9':
  2297. case '-':
  2298. case '#': /* For symbol identification (used in
  2299. live ranges). */
  2300. continue;
  2301. case ':':
  2302. /* It is a C++ nested symbol. We don't need to record it
  2303. (I don't think); if we try to look up foo::bar::baz,
  2304. then symbols for the symtab containing foo should get
  2305. read in, I think. */
  2306. /* Someone says sun cc puts out symbols like
  2307. /foo/baz/maclib::/usr/local/bin/maclib,
  2308. which would get here with a symbol type of ':'. */
  2309. continue;
  2310. default:
  2311. /* Unexpected symbol descriptor. The second and
  2312. subsequent stabs of a continued stab can show up
  2313. here. The question is whether they ever can mimic
  2314. a normal stab--it would be nice if not, since we
  2315. certainly don't want to spend the time searching to
  2316. the end of every string looking for a
  2317. backslash. */
  2318. complaint (_("unknown symbol descriptor `%c'"), p[1]);
  2319. /* Ignore it; perhaps it is an extension that we don't
  2320. know about. */
  2321. continue;
  2322. }
  2323. }
  2324. }
  2325. }
  2326. if (pst)
  2327. {
  2328. xcoff_end_psymtab (objfile, partial_symtabs,
  2329. pst, psymtab_include_list, includes_used,
  2330. ssymnum, dependency_list,
  2331. dependencies_used, textlow_not_set);
  2332. }
  2333. /* Record the toc offset value of this symbol table into objfile
  2334. structure. If no XMC_TC0 is found, toc_offset should be zero.
  2335. Another place to obtain this information would be file auxiliary
  2336. header. */
  2337. XCOFF_DATA (objfile)->toc_offset = toc_offset;
  2338. }
  2339. /* Return the toc offset value for a given objfile. */
  2340. CORE_ADDR
  2341. xcoff_get_toc_offset (struct objfile *objfile)
  2342. {
  2343. if (objfile)
  2344. return XCOFF_DATA (objfile)->toc_offset;
  2345. return 0;
  2346. }
  2347. /* Scan and build partial symbols for a symbol file.
  2348. We have been initialized by a call to dbx_symfile_init, which
  2349. put all the relevant info into a "struct dbx_symfile_info",
  2350. hung off the objfile structure.
  2351. SECTION_OFFSETS contains offsets relative to which the symbols in the
  2352. various sections are (depending where the sections were actually
  2353. loaded). */
  2354. static void
  2355. xcoff_initial_scan (struct objfile *objfile, symfile_add_flags symfile_flags)
  2356. {
  2357. bfd *abfd;
  2358. int val;
  2359. int num_symbols; /* # of symbols */
  2360. file_ptr symtab_offset; /* symbol table and */
  2361. file_ptr stringtab_offset; /* string table file offsets */
  2362. struct xcoff_symfile_info *info;
  2363. const char *name;
  2364. unsigned int size;
  2365. info = XCOFF_DATA (objfile);
  2366. symfile_bfd = abfd = objfile->obfd;
  2367. name = objfile_name (objfile);
  2368. num_symbols = bfd_get_symcount (abfd); /* # of symbols */
  2369. symtab_offset = obj_sym_filepos (abfd); /* symbol table file offset */
  2370. stringtab_offset = symtab_offset +
  2371. num_symbols * coff_data (abfd)->local_symesz;
  2372. info->min_lineno_offset = 0;
  2373. info->max_lineno_offset = 0;
  2374. bfd_map_over_sections (abfd, find_linenos, info);
  2375. if (num_symbols > 0)
  2376. {
  2377. /* Read the string table. */
  2378. init_stringtab (abfd, stringtab_offset, objfile);
  2379. /* Read the .debug section, if present and if we're not ignoring
  2380. it. */
  2381. if (!(objfile->flags & OBJF_READNEVER))
  2382. {
  2383. struct bfd_section *secp;
  2384. bfd_size_type length;
  2385. bfd_byte *debugsec = NULL;
  2386. secp = bfd_get_section_by_name (abfd, ".debug");
  2387. if (secp)
  2388. {
  2389. length = bfd_section_size (secp);
  2390. if (length)
  2391. {
  2392. debugsec
  2393. = (bfd_byte *) obstack_alloc (&objfile->objfile_obstack,
  2394. length);
  2395. if (!bfd_get_full_section_contents (abfd, secp, &debugsec))
  2396. {
  2397. error (_("Error reading .debug section of `%s': %s"),
  2398. name, bfd_errmsg (bfd_get_error ()));
  2399. }
  2400. }
  2401. }
  2402. info->debugsec = (char *) debugsec;
  2403. }
  2404. }
  2405. /* Read the symbols. We keep them in core because we will want to
  2406. access them randomly in read_symbol*. */
  2407. val = bfd_seek (abfd, symtab_offset, SEEK_SET);
  2408. if (val < 0)
  2409. error (_("Error reading symbols from %s: %s"),
  2410. name, bfd_errmsg (bfd_get_error ()));
  2411. size = coff_data (abfd)->local_symesz * num_symbols;
  2412. info->symtbl = (char *) obstack_alloc (&objfile->objfile_obstack, size);
  2413. info->symtbl_num_syms = num_symbols;
  2414. val = bfd_bread (info->symtbl, size, abfd);
  2415. if (val != size)
  2416. perror_with_name (_("reading symbol table"));
  2417. scoped_free_pendings free_pending;
  2418. minimal_symbol_reader reader (objfile);
  2419. /* Now that the symbol table data of the executable file are all in core,
  2420. process them and define symbols accordingly. */
  2421. psymbol_functions *psf = new psymbol_functions ();
  2422. psymtab_storage *partial_symtabs = psf->get_partial_symtabs ().get ();
  2423. objfile->qf.emplace_front (psf);
  2424. scan_xcoff_symtab (reader, partial_symtabs, objfile);
  2425. /* Install any minimal symbols that have been collected as the current
  2426. minimal symbols for this objfile. */
  2427. reader.install ();
  2428. /* DWARF2 sections. */
  2429. if (dwarf2_has_info (objfile, &dwarf2_xcoff_names))
  2430. dwarf2_build_psymtabs (objfile);
  2431. dwarf2_build_frame_info (objfile);
  2432. }
  2433. static void
  2434. xcoff_symfile_offsets (struct objfile *objfile,
  2435. const section_addr_info &addrs)
  2436. {
  2437. const char *first_section_name;
  2438. default_symfile_offsets (objfile, addrs);
  2439. /* Oneof the weird side-effects of default_symfile_offsets is that
  2440. it sometimes sets some section indices to zero for sections that,
  2441. in fact do not exist. See the body of default_symfile_offsets
  2442. for more info on when that happens. Undo that, as this then allows
  2443. us to test whether the associated section exists or not, and then
  2444. access it quickly (without searching it again). */
  2445. if (objfile->section_offsets.empty ())
  2446. return; /* Is that even possible? Better safe than sorry. */
  2447. first_section_name = bfd_section_name (objfile->sections[0].the_bfd_section);
  2448. if (objfile->sect_index_text == 0
  2449. && strcmp (first_section_name, ".text") != 0)
  2450. objfile->sect_index_text = -1;
  2451. if (objfile->sect_index_data == 0
  2452. && strcmp (first_section_name, ".data") != 0)
  2453. objfile->sect_index_data = -1;
  2454. if (objfile->sect_index_bss == 0
  2455. && strcmp (first_section_name, ".bss") != 0)
  2456. objfile->sect_index_bss = -1;
  2457. if (objfile->sect_index_rodata == 0
  2458. && strcmp (first_section_name, ".rodata") != 0)
  2459. objfile->sect_index_rodata = -1;
  2460. }
  2461. /* Register our ability to parse symbols for xcoff BFD files. */
  2462. static const struct sym_fns xcoff_sym_fns =
  2463. {
  2464. /* It is possible that coff and xcoff should be merged as
  2465. they do have fundamental similarities (for example, the extra storage
  2466. classes used for stabs could presumably be recognized in any COFF file).
  2467. However, in addition to obvious things like all the csect hair, there are
  2468. some subtler differences between xcoffread.c and coffread.c, notably
  2469. the fact that coffread.c has no need to read in all the symbols, but
  2470. xcoffread.c reads all the symbols and does in fact randomly access them
  2471. (in C_BSTAT and line number processing). */
  2472. xcoff_new_init, /* init anything gbl to entire symtab */
  2473. xcoff_symfile_init, /* read initial info, setup for sym_read() */
  2474. xcoff_initial_scan, /* read a symbol file into symtab */
  2475. xcoff_symfile_finish, /* finished with file, cleanup */
  2476. xcoff_symfile_offsets, /* xlate offsets ext->int form */
  2477. default_symfile_segments, /* Get segment information from a file. */
  2478. aix_process_linenos,
  2479. default_symfile_relocate, /* Relocate a debug section. */
  2480. NULL, /* sym_probe_fns */
  2481. };
  2482. /* Same as xcoff_get_n_import_files, but for core files. */
  2483. static int
  2484. xcoff_get_core_n_import_files (bfd *abfd)
  2485. {
  2486. asection *sect = bfd_get_section_by_name (abfd, ".ldinfo");
  2487. gdb_byte buf[4];
  2488. file_ptr offset = 0;
  2489. int n_entries = 0;
  2490. if (sect == NULL)
  2491. return -1; /* Not a core file. */
  2492. for (offset = 0; offset < bfd_section_size (sect);)
  2493. {
  2494. int next;
  2495. n_entries++;
  2496. if (!bfd_get_section_contents (abfd, sect, buf, offset, 4))
  2497. return -1;
  2498. next = bfd_get_32 (abfd, buf);
  2499. if (next == 0)
  2500. break; /* This is the last entry. */
  2501. offset += next;
  2502. }
  2503. /* Return the number of entries, excluding the first one, which is
  2504. the path to the executable that produced this core file. */
  2505. return n_entries - 1;
  2506. }
  2507. /* Return the number of import files (shared libraries) that the given
  2508. BFD depends on. Return -1 if this number could not be computed. */
  2509. int
  2510. xcoff_get_n_import_files (bfd *abfd)
  2511. {
  2512. asection *sect = bfd_get_section_by_name (abfd, ".loader");
  2513. gdb_byte buf[4];
  2514. int l_nimpid;
  2515. /* If the ".loader" section does not exist, the objfile is probably
  2516. not an executable. Might be a core file... */
  2517. if (sect == NULL)
  2518. return xcoff_get_core_n_import_files (abfd);
  2519. /* The number of entries in the Import Files Table is stored in
  2520. field l_nimpid. This field is always at offset 16, and is
  2521. always 4 bytes long. Read those 4 bytes. */
  2522. if (!bfd_get_section_contents (abfd, sect, buf, 16, 4))
  2523. return -1;
  2524. l_nimpid = bfd_get_32 (abfd, buf);
  2525. /* By convention, the first entry is the default LIBPATH value
  2526. to be used by the system loader, so it does not count towards
  2527. the number of import files. */
  2528. return l_nimpid - 1;
  2529. }
  2530. void _initialize_xcoffread ();
  2531. void
  2532. _initialize_xcoffread ()
  2533. {
  2534. add_symtab_fns (bfd_target_xcoff_flavour, &xcoff_sym_fns);
  2535. }