dbxread.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057
  1. /* Read dbx symbol tables and convert to internal format, for GDB.
  2. Copyright (C) 1986, 1987 Free Software Foundation, Inc.
  3. GDB is distributed in the hope that it will be useful, but WITHOUT ANY
  4. WARRANTY. No author or distributor accepts responsibility to anyone
  5. for the consequences of using it or for whether it serves any
  6. particular purpose or works at all, unless he says so in writing.
  7. Refer to the GDB General Public License for full details.
  8. Everyone is granted permission to copy, modify and redistribute GDB,
  9. but only under the conditions described in the GDB General Public
  10. License. A copy of this license is supposed to have been given to you
  11. along with GDB so you can know your rights and responsibilities. It
  12. should be in a file named COPYING. Among other things, the copyright
  13. notice and this notice must be preserved on all copies.
  14. In other words, go ahead and share GDB, but don't try to stop
  15. anyone else from sharing it farther. Help stamp out software hoarding!
  16. */
  17. #include <a.out.h>
  18. #include <stab.h>
  19. #include <stdio.h>
  20. #include <obstack.h>
  21. #include <sys/param.h>
  22. #include <sys/file.h>
  23. #include "defs.h"
  24. #include "initialize.h"
  25. #include "symtab.h"
  26. #include "param.h"
  27. static void add_symbol_to_list ();
  28. static void read_dbx_symtab ();
  29. static void process_one_symbol ();
  30. static struct type *read_type ();
  31. static struct type *read_range_type ();
  32. static struct type *read_enum_type ();
  33. static struct type *read_struct_type ();
  34. static long read_number ();
  35. static void finish_block ();
  36. static struct blockvector *make_blockvector ();
  37. static struct symbol *define_symbol ();
  38. static void start_subfile ();
  39. static int hashname ();
  40. static void hash_symsegs ();
  41. extern struct symtab *read_symsegs ();
  42. START_FILE
  43. /* Chain of symtabs made from reading the file's symsegs.
  44. These symtabs do not go into symtab_list themselves,
  45. but the information is copied from them when appropriate
  46. to make the symtabs that will exist permanently. */
  47. static struct symtab *symseg_chain;
  48. /* Symseg symbol table for the file whose data we are now processing.
  49. It is one of those in symseg_chain. Or 0, for a compilation that
  50. has no symseg. */
  51. static struct symtab *current_symseg;
  52. /* Name of source file whose symbol data we are now processing.
  53. This comes from a symbol of type N_SO. */
  54. static char *last_source_file;
  55. /* Core address of start of text of current source file.
  56. This too comes from the N_SO symbol. */
  57. static CORE_ADDR last_source_start_addr;
  58. /* End of the text segment of the executable file,
  59. as found in the symbol _etext. */
  60. static CORE_ADDR end_of_text_addr;
  61. /* The list of sub-source-files within the current individual compilation.
  62. Each file gets its own symtab with its own linetable and associated info,
  63. but they all share one blockvector. */
  64. struct subfile
  65. {
  66. struct subfile *next;
  67. char *name;
  68. struct linetable *line_vector;
  69. int line_vector_length;
  70. int line_vector_index;
  71. int prev_line_number;
  72. };
  73. static struct subfile *subfiles;
  74. static struct subfile *current_subfile;
  75. /* The addresses of the symbol table stream and the string table
  76. of the object file we are reading (as copied into core). */
  77. static FILE *nlist_stream_global;
  78. static int nlist_size_global;
  79. static char *stringtab_global;
  80. /* The index in nlist_global of the last dbx symbol to be processed. */
  81. static int symnum;
  82. /* Vector of types defined so far, indexed by their dbx type numbers.
  83. (In newer sun systems, dbx uses a pair of numbers in parens,
  84. as in "(SUBFILENUM,NUMWITHINSUBFILE)". Then these numbers must be
  85. translated through the type_translations hash table to get
  86. the index into the type vector.) */
  87. static struct typevector *type_vector;
  88. /* Number of elements allocated for type_vector currently. */
  89. static int type_vector_length;
  90. /* Vector of line number information. */
  91. static struct linetable *line_vector;
  92. /* Index of next entry to go in line_vector_index. */
  93. static int line_vector_index;
  94. /* Last line number recorded in the line vector. */
  95. static int prev_line_number;
  96. /* Number of elements allocated for line_vector currently. */
  97. static int line_vector_length;
  98. /* Chain of global symbols whose values are not known yet.
  99. They are chained thru the SYMBOL_VALUE, since we don't
  100. have the correct data for that slot yet. */
  101. #define HASHSIZE 127
  102. static struct symbol *global_sym_chain[HASHSIZE];
  103. /* Record the symbols defined for each context in a list.
  104. We don't create a struct block for the context until we
  105. know how long to make it. */
  106. struct pending
  107. {
  108. struct pending *next;
  109. struct symbol *symbol;
  110. };
  111. /* Here are the three lists that symbols are put on. */
  112. struct pending *file_symbols; /* static at top level, and types */
  113. struct pending *global_symbols; /* global functions and variables */
  114. struct pending *local_symbols; /* everything local to lexical context */
  115. /* List of unclosed lexical contexts
  116. (that will become blocks, eventually). */
  117. struct context_stack
  118. {
  119. struct context_stack *next;
  120. struct pending *locals;
  121. struct pending_block *old_blocks;
  122. struct symbol *name;
  123. CORE_ADDR start_addr;
  124. int depth;
  125. };
  126. struct context_stack *context_stack;
  127. /* Nonzero if within a function (so symbols should be local,
  128. if nothing says specifically). */
  129. int within_function;
  130. /* List of blocks already made (lexical contexts already closed).
  131. This is used at the end to make the blockvector. */
  132. struct pending_block
  133. {
  134. struct pending_block *next;
  135. struct block *block;
  136. };
  137. struct pending_block *pending_blocks;
  138. extern CORE_ADDR first_object_file_end; /* From blockframe.c */
  139. /* File name symbols were loaded from. */
  140. static char *symfile;
  141. /* Support for Sun changes to dbx symbol format */
  142. /* For each identified header file, we have a table of types defined
  143. in that header file.
  144. header_files maps header file names to their type tables.
  145. It is a vector of n_header_files elements.
  146. Each element describes one header file.
  147. It contains a vector of types.
  148. Sometimes it can happen that the same header file produces
  149. different results when included in different places.
  150. This can result from conditionals or from different
  151. things done before including the file.
  152. When this happens, there are multiple entries for the file in this table,
  153. one entry for each distinct set of results.
  154. The entries are distinguished by the INSTANCE field.
  155. The INSTANCE field appears in the N_BINCL and N_EXCL symbol table and is
  156. used to match header-file references to their corresponding data. */
  157. struct header_file
  158. {
  159. char *name; /* Name of header file */
  160. int instance; /* Numeric code distinguishing instances
  161. of one header file that produced
  162. different results when included.
  163. It comes from the N_BINCL or N_EXCL. */
  164. struct type **vector; /* Pointer to vector of types */
  165. int length; /* Allocated length (# elts) of that vector */
  166. };
  167. static struct header_file *header_files;
  168. static int n_header_files;
  169. static int n_allocated_header_files;
  170. /* Within each object file, various header files are assigned numbers.
  171. A type is defined or referred to with a pair of numbers
  172. (FILENUM,TYPENUM) where FILENUM is the number of the header file
  173. and TYPENUM is the number within that header file.
  174. TYPENUM is the index within the vector of types for that header file.
  175. FILENUM == 1 is special; it refers to the main source of the object file,
  176. and not to any header file. FILENUM != 1 is interpreted by looking it up
  177. in the following table, which contains indices in header_files. */
  178. static int *this_object_header_files;
  179. static int n_this_object_header_files;
  180. static int n_allocated_this_object_header_files;
  181. /* When a header file is getting special overriding definitions
  182. for one source file, record here the header_files index
  183. of its normal definition vector.
  184. At other times, this is -1. */
  185. static int header_file_prev_index;
  186. /* At the start of reading dbx symbols, allocate our tables. */
  187. static void
  188. init_header_files ()
  189. {
  190. n_allocated_header_files = 10;
  191. header_files = (struct header_file *) xmalloc (10 * sizeof (struct header_file));
  192. n_header_files = 0;
  193. n_allocated_this_object_header_files = 10;
  194. this_object_header_files = (int *) xmalloc (10 * sizeof (int));
  195. }
  196. /* At the end of reading dbx symbols, free our tables. */
  197. static void
  198. free_header_files ()
  199. {
  200. register int i;
  201. for (i = 0; i < n_header_files; i++)
  202. free (header_files[i].name);
  203. free (header_files);
  204. free (this_object_header_files);
  205. }
  206. /* Called at the start of each object file's symbols.
  207. Clear out the mapping of header file numbers to header files. */
  208. static void
  209. new_object_header_files ()
  210. {
  211. /* Leave FILENUM of 0 free for builtin types and this file's types. */
  212. n_this_object_header_files = 1;
  213. header_file_prev_index = -1;
  214. }
  215. /* Add header file number I for this object file
  216. at the next successive FILENUM. */
  217. static void
  218. add_this_object_header_file (i)
  219. int i;
  220. {
  221. if (n_this_object_header_files == n_allocated_this_object_header_files)
  222. {
  223. n_allocated_this_object_header_files *= 2;
  224. this_object_header_files
  225. = (int *) xrealloc (this_object_header_files,
  226. n_allocated_this_object_header_files * sizeof (int));
  227. }
  228. this_object_header_files[n_this_object_header_files++] = i;
  229. }
  230. /* Add to this file an "old" header file, one already seen in
  231. a previous object file. NAME is the header file's name.
  232. INSTANCE is its instance code, to select among multiple
  233. symbol tables for the same header file. */
  234. static void
  235. add_old_header_file (name, instance)
  236. char *name;
  237. int instance;
  238. {
  239. register struct header_file *p = header_files;
  240. register int i;
  241. for (i = 0; i < n_header_files; i++)
  242. if (!strcmp (p[i].name, name) && instance == p[i].instance)
  243. {
  244. add_this_object_header_file (i);
  245. return;
  246. }
  247. error ("Invalid symbol data: \"repeated\" header file that hasn't been seen before, at symtab pos %d.",
  248. symnum);
  249. }
  250. /* Add to this file a "new" header file: definitions for its types follow.
  251. NAME is the header file's name.
  252. Most often this happens only once for each distinct header file,
  253. but not necessarily. If it happens more than once, INSTANCE has
  254. a different value each time, and references to the header file
  255. use INSTANCE values to select among them.
  256. dbx output contains "begin" and "end" markers for each new header file,
  257. but at this level we just need to know which files there have been;
  258. so we record the file when its "begin" is seen and ignore the "end". */
  259. static void
  260. add_new_header_file (name, instance)
  261. char *name;
  262. int instance;
  263. {
  264. register int i;
  265. register struct header_file *p = header_files;
  266. header_file_prev_index = -1;
  267. #if 0
  268. /* This code was used before I knew about the instance codes.
  269. My first hypothesis is that it is not necessary now
  270. that instance codes are handled. */
  271. /* Has this header file a previous definition?
  272. If so, make a new entry anyway so that this use in this source file
  273. gets a separate entry. Later source files get the old entry.
  274. Record here the index of the old entry, so that any type indices
  275. not previously defined can get defined in the old entry as
  276. well as in the new one. */
  277. for (i = 0; i < n_header_files; i++)
  278. if (!strcmp (p[i].name, name))
  279. {
  280. header_file_prev_index = i;
  281. }
  282. #endif
  283. /* Make sure there is room for one more header file. */
  284. if (n_header_files == n_allocated_header_files)
  285. {
  286. n_allocated_header_files *= 2;
  287. header_files
  288. = (struct header_file *) xrealloc (header_files, n_allocated_header_files * sizeof (struct header_file));
  289. }
  290. /* Create an entry for this header file. */
  291. i = n_header_files++;
  292. header_files[i].name = name;
  293. header_files[i].instance = instance;
  294. header_files[i].length = 10;
  295. header_files[i].vector
  296. = (struct type **) xmalloc (10 * sizeof (struct type *));
  297. bzero (header_files[i].vector, 10 * sizeof (struct type *));
  298. add_this_object_header_file (i);
  299. }
  300. /* Look up a dbx type-number pair. Return the address of the slot
  301. where the type for that number-pair is stored.
  302. The number-pair is in TYPENUMS.
  303. This can be used for finding the type associated with that pair
  304. or for associating a new type with the pair. */
  305. static struct type **
  306. dbx_lookup_type (typenums)
  307. int typenums[2];
  308. {
  309. register int filenum = typenums[0], index = typenums[1];
  310. if (filenum < 0 || filenum >= n_this_object_header_files)
  311. error ("Invalid symbol data: type number (%d,%d) out of range at symtab pos %d.",
  312. filenum, index, symnum);
  313. if (filenum == 0)
  314. {
  315. /* Type is defined outside of header files.
  316. Find it in this object file's type vector. */
  317. if (index >= type_vector_length)
  318. {
  319. type_vector_length *= 2;
  320. type_vector = (struct typevector *)
  321. xrealloc (type_vector, sizeof (struct typevector) + type_vector_length * sizeof (struct type *));
  322. bzero (&type_vector->type[type_vector_length / 2],
  323. type_vector_length * sizeof (struct type *) / 2);
  324. }
  325. return &type_vector->type[index];
  326. }
  327. else
  328. {
  329. register int real_filenum = this_object_header_files[filenum];
  330. register struct header_file *f;
  331. if (real_filenum >= n_header_files)
  332. abort ();
  333. f = &header_files[real_filenum];
  334. if (index >= f->length)
  335. {
  336. f->length *= 2;
  337. f->vector = (struct type **)
  338. xrealloc (f->vector, f->length * sizeof (struct type *));
  339. bzero (&f->vector[f->length / 2],
  340. f->length * sizeof (struct type *) / 2);
  341. }
  342. return &f->vector[index];
  343. }
  344. }
  345. /* Make sure there is a type allocated for type numbers TYPENUMS
  346. and return the type object.
  347. This can create an empty (zeroed) type object. */
  348. static struct type *
  349. dbx_alloc_type (typenums)
  350. int typenums[2];
  351. {
  352. register struct type **type_addr = dbx_lookup_type (typenums);
  353. register struct type *type = *type_addr;
  354. /* If we are referring to a type not known at all yet,
  355. allocate an empty type for it.
  356. We will fill it in later if we find out how. */
  357. if (type == 0)
  358. {
  359. type = (struct type *) obstack_alloc (symbol_obstack,
  360. sizeof (struct type));
  361. bzero (type, sizeof (struct type));
  362. *type_addr = type;
  363. }
  364. return type;
  365. }
  366. #if 0
  367. static struct type **
  368. explicit_lookup_type (real_filenum, index)
  369. int real_filenum, index;
  370. {
  371. register struct header_file *f = &header_files[real_filenum];
  372. if (index >= f->length)
  373. {
  374. f->length *= 2;
  375. f->vector = (struct type **)
  376. xrealloc (f->vector, f->length * sizeof (struct type *));
  377. bzero (&f->vector[f->length / 2],
  378. f->length * sizeof (struct type *) / 2);
  379. }
  380. return &f->vector[index];
  381. }
  382. #endif
  383. /* maintain the lists of symbols and blocks */
  384. /* Add a symbol to one of the lists of symbols. */
  385. static void
  386. add_symbol_to_list (symbol, listhead)
  387. struct symbol *symbol;
  388. struct pending **listhead;
  389. {
  390. register struct pending *link
  391. = (struct pending *) xmalloc (sizeof (struct pending));
  392. link->next = *listhead;
  393. link->symbol = symbol;
  394. *listhead = link;
  395. }
  396. /* Take one of the lists of symbols and make a block from it.
  397. Put the block on the list of pending blocks. */
  398. static void
  399. finish_block (symbol, listhead, old_blocks, start, end)
  400. struct symbol *symbol;
  401. struct pending **listhead;
  402. struct pending_block *old_blocks;
  403. CORE_ADDR start, end;
  404. {
  405. register struct pending *next, *next1;
  406. register struct block *block;
  407. register struct pending_block *pblock;
  408. struct pending_block *opblock;
  409. register int i;
  410. /* Count the length of the list of symbols. */
  411. for (next = *listhead, i = 0; next; next = next->next, i++);
  412. block = (struct block *) xmalloc (sizeof (struct block) + (i - 1) * sizeof (struct symbol *));
  413. /* Copy the symbols into the block. */
  414. BLOCK_NSYMS (block) = i;
  415. for (next = *listhead; next; next = next->next)
  416. BLOCK_SYM (block, --i) = next->symbol;
  417. BLOCK_START (block) = start;
  418. BLOCK_END (block) = end;
  419. BLOCK_SUPERBLOCK (block) = 0; /* Filled in when containing block is made */
  420. /* Put the block in as the value of the symbol that names it. */
  421. if (symbol)
  422. {
  423. SYMBOL_BLOCK_VALUE (symbol) = block;
  424. BLOCK_FUNCTION (block) = symbol;
  425. }
  426. else
  427. BLOCK_FUNCTION (block) = 0;
  428. /* Now free the links of the list, and empty the list. */
  429. for (next = *listhead; next; next = next1)
  430. {
  431. next1 = next->next;
  432. free (next);
  433. }
  434. *listhead = 0;
  435. /* Install this block as the superblock
  436. of all blocks made since the start of this scope
  437. that don't have superblocks yet. */
  438. opblock = 0;
  439. for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
  440. {
  441. if (BLOCK_SUPERBLOCK (pblock->block) == 0)
  442. BLOCK_SUPERBLOCK (pblock->block) = block;
  443. opblock = pblock;
  444. }
  445. /* Record this block on the list of all blocks in the file.
  446. Put it after opblock, or at the beginning if opblock is 0.
  447. This puts the block in the list after all its subblocks. */
  448. pblock = (struct pending_block *) xmalloc (sizeof (struct pending_block));
  449. pblock->block = block;
  450. if (opblock)
  451. {
  452. pblock->next = opblock->next;
  453. opblock->next = pblock;
  454. }
  455. else
  456. {
  457. pblock->next = pending_blocks;
  458. pending_blocks = pblock;
  459. }
  460. }
  461. static struct blockvector *
  462. make_blockvector ()
  463. {
  464. register struct pending_block *next, *next1;
  465. register struct blockvector *blockvector;
  466. register int i;
  467. /* Count the length of the list of blocks. */
  468. for (next = pending_blocks, i = 0; next; next = next->next, i++);
  469. blockvector = (struct blockvector *) xmalloc (sizeof (struct blockvector) + (i - 1) * sizeof (struct block *));
  470. /* Copy the blocks into the blockvector.
  471. This is done in reverse order, which happens to put
  472. the blocks into the proper order (ascending starting address).
  473. finish_block has hair to insert each block into the list
  474. after its subblocks in order to make sure this is true. */
  475. BLOCKVECTOR_NBLOCKS (blockvector) = i;
  476. for (next = pending_blocks; next; next = next->next)
  477. BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
  478. /* Now free the links of the list, and empty the list. */
  479. for (next = pending_blocks; next; next = next1)
  480. {
  481. next1 = next->next;
  482. free (next);
  483. }
  484. pending_blocks = 0;
  485. return blockvector;
  486. }
  487. /* Manage the vector of line numbers. */
  488. static
  489. record_line (line, pc)
  490. int line;
  491. CORE_ADDR pc;
  492. {
  493. /* Ignore the dummy line number in libg.o */
  494. if (line == 0xffff)
  495. return;
  496. /* Make sure line vector is big enough. */
  497. if (line_vector_index + 1 >= line_vector_length)
  498. {
  499. line_vector_length *= 2;
  500. line_vector = (struct linetable *)
  501. xrealloc (line_vector,
  502. sizeof (struct linetable) + line_vector_length * sizeof (int));
  503. current_subfile->line_vector = line_vector;
  504. }
  505. /* If this line is not continguous with previous one recorded,
  506. record a line-number entry for it. */
  507. if (line != prev_line_number + 1)
  508. line_vector->item[line_vector_index++] = - line;
  509. prev_line_number = line;
  510. /* Record the core address of the line. */
  511. line_vector->item[line_vector_index++] = pc;
  512. }
  513. /* Start a new symtab for a new source file.
  514. This is called when a dbx symbol of type N_SO is seen;
  515. it indicates the start of data for one original source file. */
  516. static void
  517. start_symtab (name, start_addr)
  518. char *name;
  519. CORE_ADDR start_addr;
  520. {
  521. register struct symtab *s;
  522. last_source_file = name;
  523. last_source_start_addr = start_addr;
  524. file_symbols = 0;
  525. global_symbols = 0;
  526. context_stack = 0;
  527. within_function = 0;
  528. new_object_header_files ();
  529. for (s = symseg_chain; s; s = s->next)
  530. if (s->ldsymoff == symnum * sizeof (struct nlist))
  531. break;
  532. current_symseg = s;
  533. type_vector_length = 160;
  534. type_vector = (struct typevector *) xmalloc (sizeof (struct typevector) + type_vector_length * sizeof (struct type *));
  535. bzero (type_vector->type, type_vector_length * sizeof (struct type *));
  536. /* Initialize the list of sub source files with one entry
  537. for this file (the top-level source file). */
  538. subfiles = 0;
  539. current_subfile = 0;
  540. start_subfile (name);
  541. }
  542. /* Handle an N_SOL symbol, which indicates the start of
  543. code that came from an included (or otherwise merged-in)
  544. source file with a different name. */
  545. static void
  546. start_subfile (name)
  547. char *name;
  548. {
  549. register struct subfile *subfile;
  550. /* Save the current subfile's line vector data. */
  551. if (current_subfile)
  552. {
  553. current_subfile->line_vector_index = line_vector_index;
  554. current_subfile->line_vector_length = line_vector_length;
  555. current_subfile->prev_line_number = prev_line_number;
  556. }
  557. /* See if this subfile is already known as a subfile of the
  558. current main source file. */
  559. for (subfile = subfiles; subfile; subfile = subfile->next)
  560. {
  561. if (!strcmp (subfile->name, name))
  562. {
  563. line_vector = subfile->line_vector;
  564. line_vector_index = subfile->line_vector_index;
  565. line_vector_length = subfile->line_vector_length;
  566. prev_line_number = subfile->prev_line_number;
  567. current_subfile = subfile;
  568. return;
  569. }
  570. }
  571. /* This subfile is not known. Add an entry for it. */
  572. line_vector_index = 0;
  573. line_vector_length = 1000;
  574. prev_line_number = -2; /* Force first line number to be explicit */
  575. line_vector = (struct linetable *)
  576. xmalloc (sizeof (struct linetable) + line_vector_length * sizeof (int));
  577. /* Make an entry for this subfile in the list of all subfiles
  578. of the current main source file. */
  579. subfile = (struct subfile *) xmalloc (sizeof (struct subfile));
  580. subfile->next = subfiles;
  581. subfile->name = savestring (name, strlen (name));
  582. subfile->line_vector = line_vector;
  583. subfiles = subfile;
  584. current_subfile = subfile;
  585. }
  586. /* Finish the symbol definitions for one main source file,
  587. close off all the lexical contexts for that file
  588. (creating struct block's for them), then make the struct symtab
  589. for that file and put it in the list of all such.
  590. END_ADDR is the address of the end of the file's text. */
  591. static void
  592. end_symtab (end_addr)
  593. CORE_ADDR end_addr;
  594. {
  595. register struct symtab *symtab;
  596. register struct context_stack *cstk;
  597. register struct blockvector *blockvector;
  598. register struct subfile *subfile;
  599. register struct linetable *lv;
  600. struct subfile *nextsub;
  601. /* Finish the lexical context of the last function in the file. */
  602. if (context_stack)
  603. {
  604. cstk = context_stack;
  605. /* Make a block for the local symbols within. */
  606. finish_block (cstk->name, &local_symbols, cstk->old_blocks,
  607. cstk->start_addr, end_addr);
  608. free (cstk);
  609. }
  610. /* Finish defining all the blocks of this symtab. */
  611. if (current_symseg == 0)
  612. {
  613. finish_block (0, &file_symbols, 0, last_source_start_addr, end_addr);
  614. finish_block (0, &global_symbols, 0, last_source_start_addr, end_addr);
  615. blockvector = make_blockvector ();
  616. }
  617. current_subfile->line_vector_index = line_vector_index;
  618. /* Now create the symtab objects proper, one for each subfile. */
  619. /* (The main file is one of them.) */
  620. for (subfile = subfiles; subfile; subfile = nextsub)
  621. {
  622. symtab = (struct symtab *) xmalloc (sizeof (struct symtab));
  623. /* Fill in its components. */
  624. if (current_symseg)
  625. {
  626. bcopy (current_symseg, symtab, sizeof (struct symtab));
  627. }
  628. else
  629. {
  630. symtab->blockvector = blockvector;
  631. type_vector->length = type_vector_length;
  632. symtab->typevector = type_vector;
  633. symtab->free_code = free_linetable;
  634. if (subfile->next == 0)
  635. symtab->free_code = free_contents;
  636. symtab->free_ptr = 0;
  637. }
  638. symtab->filename = subfile->name;
  639. lv = subfile->line_vector;
  640. lv->nitems = subfile->line_vector_index;
  641. symtab->linetable = (struct linetable *)
  642. xrealloc (lv, sizeof (struct linetable) + lv->nitems * sizeof (int));
  643. symtab->nlines = 0;
  644. symtab->line_charpos = 0;
  645. /* Link the new symtab into the list of such. */
  646. symtab->next = symtab_list;
  647. symtab_list = symtab;
  648. nextsub = subfile->next;
  649. free (subfile);
  650. }
  651. type_vector = 0;
  652. type_vector_length = -1;
  653. line_vector = 0;
  654. line_vector_length = -1;
  655. last_source_file = 0;
  656. }
  657. #ifdef N_BINCL
  658. /* Handle the N_BINCL and N_EINCL symbol types
  659. that act like N_SOL for switching source files
  660. (different subfiles, as we call them) within one object file,
  661. but using a stack rather than in an arbitrary order. */
  662. struct subfile_stack
  663. {
  664. struct subfile_stack *next;
  665. char *name;
  666. int prev_index;
  667. };
  668. struct subfile_stack *subfile_stack;
  669. static void
  670. push_subfile ()
  671. {
  672. register struct subfile_stack *tem
  673. = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack));
  674. tem->next = subfile_stack;
  675. subfile_stack = tem;
  676. if (current_subfile == 0 || current_subfile->name == 0)
  677. abort ();
  678. tem->name = current_subfile->name;
  679. tem->prev_index = header_file_prev_index;
  680. }
  681. static char *
  682. pop_subfile ()
  683. {
  684. register char *name;
  685. register struct subfile_stack *link = subfile_stack;
  686. if (link == 0)
  687. abort ();
  688. name = link->name;
  689. subfile_stack = link->next;
  690. header_file_prev_index = link->prev_index;
  691. free (link);
  692. return name;
  693. }
  694. #endif /* Have N_BINCL */
  695. /* Accumulate the misc functions in bunches of 127.
  696. At the end, copy them all into one newly allocated structure. */
  697. #define MISC_BUNCH_SIZE 127
  698. struct misc_bunch
  699. {
  700. struct misc_bunch *next;
  701. struct misc_function contents[MISC_BUNCH_SIZE];
  702. };
  703. /* Bunch currently being filled up.
  704. The next field points to chain of filled bunches. */
  705. static struct misc_bunch *misc_bunch;
  706. /* Number of slots filled in current bunch. */
  707. static int misc_bunch_index;
  708. /* Total number of misc functions recorded so far. */
  709. static int misc_count;
  710. static void
  711. init_misc_functions ()
  712. {
  713. misc_count = 0;
  714. misc_bunch = 0;
  715. misc_bunch_index = MISC_BUNCH_SIZE;
  716. }
  717. static void
  718. record_misc_function (name, address)
  719. char *name;
  720. CORE_ADDR address;
  721. {
  722. register struct misc_bunch *new;
  723. if (misc_bunch_index == MISC_BUNCH_SIZE)
  724. {
  725. new = (struct misc_bunch *) xmalloc (sizeof (struct misc_bunch));
  726. misc_bunch_index = 0;
  727. new->next = misc_bunch;
  728. misc_bunch = new;
  729. }
  730. misc_bunch->contents[misc_bunch_index].name = name;
  731. misc_bunch->contents[misc_bunch_index].address = address;
  732. misc_bunch_index++;
  733. misc_count++;
  734. }
  735. static int
  736. compare_misc_functions (fn1, fn2)
  737. struct misc_function *fn1, *fn2;
  738. {
  739. return fn1->address - fn2->address;
  740. }
  741. static void
  742. discard_misc_bunches ()
  743. {
  744. register struct misc_bunch *next;
  745. while (misc_bunch)
  746. {
  747. next = misc_bunch->next;
  748. free (misc_bunch);
  749. misc_bunch = next;
  750. }
  751. }
  752. static void
  753. condense_misc_bunches ()
  754. {
  755. register int i, j;
  756. register struct misc_bunch *bunch;
  757. #ifdef NAMES_HAVE_UNDERSCORE
  758. int offset = 1;
  759. #else
  760. int offset = 0;
  761. #endif
  762. misc_function_vector
  763. = (struct misc_function *)
  764. xmalloc (misc_count * sizeof (struct misc_function));
  765. j = 0;
  766. bunch = misc_bunch;
  767. while (bunch)
  768. {
  769. for (i = 0; i < misc_bunch_index; i++)
  770. {
  771. misc_function_vector[j] = bunch->contents[i];
  772. misc_function_vector[j].name
  773. = concat (misc_function_vector[j].name
  774. + (misc_function_vector[j].name[0] == '_' ? offset : 0),
  775. "", "");
  776. j++;
  777. }
  778. bunch = bunch->next;
  779. misc_bunch_index = MISC_BUNCH_SIZE;
  780. }
  781. misc_function_count = j;
  782. /* Sort the misc functions by address. */
  783. qsort (misc_function_vector, j, sizeof (struct misc_function),
  784. compare_misc_functions);
  785. }
  786. /* Call sort_syms to sort alphabetically
  787. the symbols of each block of each symtab. */
  788. static int
  789. compare_symbols (s1, s2)
  790. struct symbol **s1, **s2;
  791. {
  792. /* Names that are less should come first. */
  793. register int namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
  794. if (namediff != 0) return namediff;
  795. /* For symbols of the same name, registers should come first. */
  796. return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
  797. - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
  798. }
  799. static void
  800. sort_syms ()
  801. {
  802. register struct symtab *s;
  803. register int i, nbl;
  804. register struct blockvector *bv;
  805. register struct block *b;
  806. for (s = symtab_list; s; s = s->next)
  807. {
  808. bv = BLOCKVECTOR (s);
  809. nbl = BLOCKVECTOR_NBLOCKS (bv);
  810. for (i = 0; i < nbl; i++)
  811. {
  812. b = BLOCKVECTOR_BLOCK (bv, i);
  813. qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
  814. sizeof (struct symbol *), compare_symbols);
  815. }
  816. }
  817. }
  818. /* This is the symbol-file command. Read the file, analyze its symbols,
  819. and add a struct symtab to symtab_list. */
  820. void
  821. symbol_file_command (name)
  822. char *name;
  823. {
  824. register int desc;
  825. struct exec hdr;
  826. struct nlist *nlist;
  827. char *stringtab;
  828. long buffer;
  829. register int val;
  830. extern void close ();
  831. struct cleanup *old_chain;
  832. dont_repeat ();
  833. if (name == 0)
  834. error_no_arg ("file to read symbols from");
  835. if (symtab_list && !query ("Load new symbol table from \"%s\"? ", name))
  836. error ("Not confirmed.");
  837. if (symfile)
  838. free (symfile);
  839. symfile = 0;
  840. {
  841. char *absolute_name;
  842. desc = openp (getenv ("PATH"), 1, name, O_RDONLY, 0, &absolute_name);
  843. if (desc < 0)
  844. perror_with_name (name);
  845. else
  846. name = absolute_name;
  847. }
  848. old_chain = make_cleanup (close, desc);
  849. make_cleanup (free_current_contents, &name);
  850. val = myread (desc, &hdr, sizeof hdr);
  851. if (val < 0)
  852. perror_with_name (name);
  853. if (N_BADMAG (hdr))
  854. error ("File \"%s\" not in executable format.", name);
  855. if (hdr.a_syms == 0)
  856. {
  857. free_all_symtabs ();
  858. printf ("%s does not have a symbol-table.\n", name);
  859. fflush (stdout);
  860. return;
  861. }
  862. /* Now read the string table, all at once. */
  863. val = lseek (desc, N_SYMOFF (hdr) + hdr.a_syms, 0);
  864. if (val < 0)
  865. perror_with_name (name);
  866. val = myread (desc, &buffer, sizeof buffer);
  867. if (val < 0)
  868. perror_with_name (name);
  869. stringtab = (char *) alloca (buffer);
  870. bcopy (&buffer, stringtab, sizeof buffer);
  871. val = myread (desc, stringtab + sizeof buffer, buffer - sizeof buffer);
  872. if (val < 0)
  873. perror_with_name (name);
  874. #ifdef READ_GDB_SYMSEGS
  875. /* That puts us at the symsegs. Read them. */
  876. symseg_chain = read_symsegs (desc, name);
  877. hash_symsegs ();
  878. #else
  879. /* Where people are using the 4.2 ld program, must not check for
  880. symsegs, because that ld puts randonm garbage at the end of
  881. the output file and that would trigger an error message. */
  882. symseg_chain = 0;
  883. #endif
  884. /* Position to read the symbol table. Do not read it all at once. */
  885. val = lseek (desc, N_SYMOFF (hdr), 0);
  886. if (val < 0)
  887. perror_with_name (name);
  888. printf ("Reading symbol data from %s...", name);
  889. fflush (stdout);
  890. /* Throw away the old symbol table. */
  891. free_all_symtabs ();
  892. init_misc_functions ();
  893. make_cleanup (discard_misc_bunches, 0);
  894. init_header_files ();
  895. make_cleanup (free_header_files, 0);
  896. /* Now that the symbol table data of the executable file are all in core,
  897. process them and define symbols accordingly. Closes desc. */
  898. read_dbx_symtab (desc, stringtab, hdr.a_syms / sizeof (struct nlist));
  899. /* Sort symbols alphabetically within each block. */
  900. sort_syms ();
  901. /* Go over the misc functions and install them in vector. */
  902. condense_misc_bunches ();
  903. /* Don't allow char * to have a typename (else would get caddr_t.) */
  904. TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
  905. /* Make a default for file to list. */
  906. select_source_symtab (symtab_list);
  907. symfile = savestring (name, strlen (name));
  908. do_cleanups (old_chain);
  909. /* Free the symtabs made by read_symsegs, but not their contents,
  910. which have been copied into symtabs on symtab_list. */
  911. while (symseg_chain)
  912. {
  913. register struct symtab *s = symseg_chain->next;
  914. free (symseg_chain);
  915. symseg_chain = s;
  916. }
  917. printf ("done.\n");
  918. fflush (stdout);
  919. }
  920. /* Return name of file symbols were loaded from, or 0 if none.. */
  921. char *
  922. get_sym_file ()
  923. {
  924. return symfile;
  925. }
  926. /* Given pointers to a a.out symbol table in core containing dbx style data,
  927. analyze them and create struct symtab's describing the symbols.
  928. NLISTLEN is the number of symbols in the symbol table.
  929. We read them one at a time using stdio.
  930. All symbol names are given as offsets relative to STRINGTAB. */
  931. static void
  932. read_dbx_symtab (desc, stringtab, nlistlen)
  933. int desc;
  934. register char *stringtab;
  935. register int nlistlen;
  936. {
  937. FILE *stream = fdopen (desc, "r");
  938. struct nlist buf;
  939. register char *namestring;
  940. register struct symbol *sym, *prev;
  941. int hash;
  942. int num_object_files = 0;
  943. #ifdef N_BINCL
  944. subfile_stack = 0;
  945. #endif
  946. nlist_stream_global = stream;
  947. nlist_size_global = nlistlen;
  948. stringtab_global = stringtab;
  949. last_source_file = 0;
  950. bzero (global_sym_chain, sizeof global_sym_chain);
  951. for (symnum = 0; symnum < nlistlen; symnum++)
  952. {
  953. fread (&buf, sizeof buf, 1, stream);
  954. namestring = buf.n_un.n_strx ? buf.n_un.n_strx + stringtab : 0;
  955. if (buf.n_type & N_STAB)
  956. process_one_symbol (buf.n_type, buf.n_desc,
  957. buf.n_value, namestring);
  958. /* A static text symbol whose name ends in ".o"
  959. can only mean the start of another object file.
  960. So end the symtab of the source file we have been processing.
  961. This is how we avoid counting the libraries as part
  962. or the last source file.
  963. Also this way we find end of first object file (crt0). */
  964. else if (buf.n_type == N_TEXT
  965. && !strcmp (namestring + strlen (namestring) - 2, ".o"))
  966. {
  967. if (num_object_files++ == 1)
  968. first_object_file_end = buf.n_value;
  969. if (last_source_file)
  970. end_symtab (buf.n_value);
  971. }
  972. else if (buf.n_type & N_EXT)
  973. {
  974. /* Record the location of _etext. */
  975. if (buf.n_type == (N_TEXT | N_EXT)
  976. && !strcmp (namestring, "_etext"))
  977. end_of_text_addr = buf.n_value;
  978. /* Defined global symbol: record as a misc function. */
  979. if (buf.n_type <= (N_TYPE | N_EXT)
  980. && buf.n_type != N_EXT)
  981. record_misc_function (namestring, buf.n_value);
  982. /* Global symbol: see if we came across a dbx definition
  983. for a corresponding symbol. If so, store the value.
  984. Remove syms from the chain when their values are stored,
  985. but search the whole chain, as there may be several syms
  986. from different files with the same name. */
  987. prev = 0;
  988. #ifdef NAMES_HAVE_UNDERSCORE
  989. hash = hashname (namestring + 1);
  990. #else /* not NAMES_HAVE_UNDERSCORE */
  991. hash = hashname (namestring);
  992. #endif /* not NAMES_HAVE_UNDERSCORE */
  993. for (sym = global_sym_chain[hash];
  994. sym;)
  995. {
  996. if (
  997. #ifdef NAMES_HAVE_UNDERSCORE
  998. *namestring == '_'
  999. && namestring[1] == SYMBOL_NAME (sym)[0]
  1000. &&
  1001. !strcmp (namestring + 2, SYMBOL_NAME (sym) + 1)
  1002. #else /* NAMES_HAVE_UNDERSCORE */
  1003. namestring[0] == SYMBOL_NAME (sym)[0]
  1004. &&
  1005. !strcmp (namestring + 1, SYMBOL_NAME (sym) + 1)
  1006. #endif /* NAMES_HAVE_UNDERSCORE */
  1007. )
  1008. {
  1009. if (prev)
  1010. SYMBOL_VALUE (prev) = SYMBOL_VALUE (sym);
  1011. else
  1012. global_sym_chain[hash]
  1013. = (struct symbol *) SYMBOL_VALUE (sym);
  1014. SYMBOL_VALUE (sym) = buf.n_value;
  1015. if (prev)
  1016. sym = (struct symbol *) SYMBOL_VALUE (prev);
  1017. else
  1018. sym = global_sym_chain[hash];
  1019. }
  1020. else
  1021. {
  1022. prev = sym;
  1023. sym = (struct symbol *) SYMBOL_VALUE (sym);
  1024. }
  1025. }
  1026. }
  1027. }
  1028. if (last_source_file)
  1029. end_symtab (end_of_text_addr);
  1030. fclose (stream);
  1031. }
  1032. /* dbx allows the text of a symbol name to be continued into the
  1033. next symbol name! When such a continuation is encountered
  1034. (a \ at the end of the text of a name)
  1035. call this function to get the continuation. */
  1036. static char *
  1037. next_symbol_text ()
  1038. {
  1039. struct nlist buf;
  1040. fread (&buf, sizeof buf, 1, nlist_stream_global);
  1041. symnum++;
  1042. return buf.n_un.n_strx + stringtab_global;
  1043. }
  1044. static int
  1045. hashname (name)
  1046. char *name;
  1047. {
  1048. register char *p = name;
  1049. register int total = p[0];
  1050. register int c;
  1051. c = p[1];
  1052. total += c << 2;
  1053. if (c)
  1054. {
  1055. c = p[2];
  1056. total += c << 4;
  1057. if (c)
  1058. total += p[3] << 6;
  1059. }
  1060. return total % HASHSIZE;
  1061. }
  1062. /* Put all appropriate global symbols in the symseg data
  1063. onto the hash chains so that their addresses will be stored
  1064. when seen later in loader global symbols. */
  1065. static void
  1066. hash_symsegs ()
  1067. {
  1068. /* Look at each symbol in each block in each symseg symtab. */
  1069. struct symtab *s;
  1070. for (s = symseg_chain; s; s = s->next)
  1071. {
  1072. register int n;
  1073. for (n = BLOCKVECTOR_NBLOCKS (BLOCKVECTOR (s)) - 1; n >= 0; n--)
  1074. {
  1075. register struct block *b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), n);
  1076. register int i;
  1077. for (i = BLOCK_NSYMS (b) - 1; i >= 0; i--)
  1078. {
  1079. register struct symbol *sym = BLOCK_SYM (b, i);
  1080. /* Put the symbol on a chain if its value is an address
  1081. that is figured out by the loader. */
  1082. if (SYMBOL_CLASS (sym) == LOC_EXTERNAL)
  1083. {
  1084. register int hash = hashname (SYMBOL_NAME (sym));
  1085. SYMBOL_VALUE (sym) = (int) global_sym_chain[hash];
  1086. global_sym_chain[hash] = sym;
  1087. SYMBOL_CLASS (sym) = LOC_STATIC;
  1088. }
  1089. }
  1090. }
  1091. }
  1092. }
  1093. static void
  1094. process_one_symbol (type, desc, value, name)
  1095. int type, desc;
  1096. CORE_ADDR value;
  1097. char *name;
  1098. {
  1099. register struct context_stack *new;
  1100. /* Something is wrong if we see real data before
  1101. seeing a source file name. */
  1102. #ifdef N_NSYMS
  1103. if (type == N_NSYMS) return;
  1104. #endif
  1105. if (type != N_SO && last_source_file == 0)
  1106. error ("Invalid symbol data: does not start by identifying a source file.");
  1107. switch (type)
  1108. {
  1109. case N_FUN:
  1110. case N_FNAME:
  1111. /* Either of these types of symbols indicates the start of
  1112. a new function. We must process its "name" normally for dbx,
  1113. but also record the start of a new lexical context, and possibly
  1114. also the end of the lexical context for the previous function. */
  1115. new = context_stack;
  1116. within_function = 1;
  1117. if (new)
  1118. {
  1119. /* Make a block for the local symbols within. */
  1120. finish_block (new->name, &local_symbols, new->old_blocks,
  1121. new->start_addr, value);
  1122. }
  1123. else
  1124. {
  1125. new = (struct context_stack *) xmalloc (sizeof (struct context_stack));
  1126. new->next = 0;
  1127. new->depth = -1;
  1128. context_stack = new;
  1129. }
  1130. new->locals = 0;
  1131. new->old_blocks = pending_blocks;
  1132. new->start_addr = value;
  1133. new->name = define_symbol (value, name);
  1134. local_symbols = 0;
  1135. break;
  1136. case N_LBRAC:
  1137. /* This "symbol" just indicates the start of an inner lexical
  1138. context within a function. */
  1139. new = (struct context_stack *) xmalloc (sizeof (struct context_stack));
  1140. new->depth = desc;
  1141. new->next = context_stack;
  1142. context_stack = new;
  1143. new->locals = local_symbols;
  1144. new->old_blocks = pending_blocks;
  1145. new->start_addr = value;
  1146. new->name = 0;
  1147. local_symbols = 0;
  1148. break;
  1149. case N_RBRAC:
  1150. /* This "symbol" just indicates the end of an inner lexical
  1151. context that was started with N_RBRAC. */
  1152. new = context_stack;
  1153. if (new == 0 || desc != new->depth)
  1154. error ("Invalid symbol data: N_LBRAC/N_RBRAC symbol mismatch, symtab pos %d.", symnum);
  1155. local_symbols = new->locals;
  1156. context_stack = new->next;
  1157. /* If this is not the outermost LBRAC...RBRAC pair in the
  1158. function, its local symbols preceded it, and are the ones
  1159. just recovered from the context stack. Defined the block for them.
  1160. If this is the outermost LBRAC...RBRAC pair, there is no
  1161. need to do anything; leave the symbols that preceded it
  1162. to be attached to the function's own block. */
  1163. if (local_symbols && context_stack->next)
  1164. {
  1165. /* Muzzle a compiler bug that makes end > start. */
  1166. if (new->start_addr > value)
  1167. new->start_addr = value;
  1168. /* Make a block for the local symbols within. */
  1169. finish_block (0, &local_symbols, new->old_blocks,
  1170. new->start_addr + last_source_start_addr,
  1171. value + last_source_start_addr);
  1172. }
  1173. free (new);
  1174. break;
  1175. case N_FN:
  1176. /* This kind of symbol supposedly indicates the start
  1177. of an object file. In fact this type does not appear. */
  1178. break;
  1179. case N_SO:
  1180. /* This type of symbol indicates the start of data
  1181. for one source file.
  1182. Finish the symbol table of the previous source file
  1183. (if any) and start accumulating a new symbol table. */
  1184. if (last_source_file)
  1185. end_symtab (value);
  1186. start_symtab (name, value);
  1187. break;
  1188. case N_SOL:
  1189. /* This type of symbol indicates the start of data for
  1190. a sub-source-file, one whose contents were copied or
  1191. included in the compilation of the main source file
  1192. (whose name was given in the N_SO symbol.) */
  1193. start_subfile (name);
  1194. break;
  1195. #ifdef N_BINCL
  1196. case N_BINCL:
  1197. push_subfile ();
  1198. add_new_header_file (name, value);
  1199. start_subfile (name);
  1200. break;
  1201. case N_EINCL:
  1202. start_subfile (pop_subfile ());
  1203. break;
  1204. case N_EXCL:
  1205. add_old_header_file (name, value);
  1206. break;
  1207. #endif /* have N_BINCL */
  1208. case N_SLINE:
  1209. /* This type of "symbol" really just records
  1210. one line-number -- core-address correspondence.
  1211. Enter it in the line list for this symbol table. */
  1212. record_line (desc, value);
  1213. break;
  1214. default:
  1215. if (name)
  1216. define_symbol (value, name);
  1217. }
  1218. }
  1219. static struct symbol *
  1220. define_symbol (value, string)
  1221. int value;
  1222. char *string;
  1223. {
  1224. register struct symbol *sym = (struct symbol *) xmalloc (sizeof (struct symbol));
  1225. char *p = (char *) index (string, ':');
  1226. int deftype;
  1227. register int i;
  1228. bzero (sym, sizeof (struct symbol));
  1229. SYMBOL_NAME (sym) = savestring (string, p - string);
  1230. p++;
  1231. /* Determine the type of name being defined. */
  1232. if ((*p >= '0' && *p <= '9') || *p == '(')
  1233. deftype = 'l';
  1234. else
  1235. deftype = *p++;
  1236. /* Now comes a number that says which data type,
  1237. and possibly more stuff to define the type
  1238. (all of which is handled by read_type) */
  1239. SYMBOL_TYPE (sym) = read_type (&p);
  1240. switch (deftype)
  1241. {
  1242. case 'f':
  1243. SYMBOL_CLASS (sym) = LOC_BLOCK;
  1244. SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  1245. add_symbol_to_list (sym, &file_symbols);
  1246. break;
  1247. case 'F':
  1248. SYMBOL_CLASS (sym) = LOC_BLOCK;
  1249. SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  1250. add_symbol_to_list (sym, &global_symbols);
  1251. break;
  1252. case 'G':
  1253. /* For a class G (global) symbol, it appears that the
  1254. value is not correct. It is necessary to search for the
  1255. corresponding linker definition to find the value.
  1256. These definitions appear at the end of the namelist. */
  1257. i = hashname (SYMBOL_NAME (sym));
  1258. SYMBOL_VALUE (sym) = (int) global_sym_chain[i];
  1259. global_sym_chain[i] = sym;
  1260. SYMBOL_CLASS (sym) = LOC_STATIC;
  1261. SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  1262. add_symbol_to_list (sym, &global_symbols);
  1263. break;
  1264. /* This case is faked by a conditional above,
  1265. when there is no code letter in the dbx data.
  1266. Dbx data never actually contains 'l'. */
  1267. case 'l':
  1268. SYMBOL_CLASS (sym) = LOC_LOCAL;
  1269. SYMBOL_VALUE (sym) = value;
  1270. SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  1271. add_symbol_to_list (sym, &local_symbols);
  1272. break;
  1273. case 'p':
  1274. SYMBOL_CLASS (sym) = LOC_ARG;
  1275. SYMBOL_VALUE (sym) = value;
  1276. SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  1277. add_symbol_to_list (sym, &local_symbols);
  1278. break;
  1279. case 'r':
  1280. SYMBOL_CLASS (sym) = LOC_REGISTER;
  1281. SYMBOL_VALUE (sym) = value;
  1282. SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  1283. add_symbol_to_list (sym, &local_symbols);
  1284. break;
  1285. case 'S':
  1286. /* Static symbol at top level of file */
  1287. SYMBOL_CLASS (sym) = LOC_STATIC;
  1288. SYMBOL_VALUE (sym) = value;
  1289. SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  1290. add_symbol_to_list (sym, &file_symbols);
  1291. break;
  1292. case 't':
  1293. SYMBOL_CLASS (sym) = LOC_TYPEDEF;
  1294. SYMBOL_VALUE (sym) = value;
  1295. SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  1296. if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0
  1297. && (TYPE_FLAGS (SYMBOL_TYPE (sym)) & TYPE_FLAG_PERM) == 0)
  1298. TYPE_NAME (SYMBOL_TYPE (sym)) = concat (SYMBOL_NAME (sym), "", "");
  1299. add_symbol_to_list (sym, &file_symbols);
  1300. break;
  1301. case 'T':
  1302. SYMBOL_CLASS (sym) = LOC_TYPEDEF;
  1303. SYMBOL_VALUE (sym) = value;
  1304. SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
  1305. if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0
  1306. && (TYPE_FLAGS (SYMBOL_TYPE (sym)) & TYPE_FLAG_PERM) == 0)
  1307. TYPE_NAME (SYMBOL_TYPE (sym))
  1308. = concat ("",
  1309. (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_ENUM
  1310. ? "enum "
  1311. : (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT
  1312. ? "struct " : "union ")),
  1313. SYMBOL_NAME (sym));
  1314. add_symbol_to_list (sym, &file_symbols);
  1315. break;
  1316. case 'V':
  1317. case 'v':
  1318. /* Static symbol of local scope */
  1319. SYMBOL_CLASS (sym) = LOC_STATIC;
  1320. SYMBOL_VALUE (sym) = value;
  1321. SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  1322. add_symbol_to_list (sym, &local_symbols);
  1323. break;
  1324. default:
  1325. error ("Invalid symbol data: unknown symbol-type code `%c' at symtab pos %d.", deftype, symnum);
  1326. }
  1327. return sym;
  1328. }
  1329. /* Read a number by which a type is referred to in dbx data,
  1330. or perhaps read a pair (FILENUM, TYPENUM) in parentheses.
  1331. Just a single number N is equivalent to (1,N).
  1332. Return the two numbers by storing them in the vector TYPENUMS.
  1333. TYPENUMS will then be used as an argument to dbx_lookup_type. */
  1334. static void
  1335. read_type_number (pp, typenums)
  1336. register char **pp;
  1337. register int *typenums;
  1338. {
  1339. if (**pp == '(')
  1340. {
  1341. (*pp)++;
  1342. typenums[0] = read_number (pp, ',');
  1343. typenums[1] = read_number (pp, ')');
  1344. }
  1345. else
  1346. {
  1347. typenums[0] = 0;
  1348. typenums[1] = read_number (pp, 0);
  1349. }
  1350. }
  1351. /* Read a dbx type reference or definition;
  1352. return the type that is meant.
  1353. This can be just a number, in which case it references
  1354. a type already defined and placed in type_vector.
  1355. Or the number can be followed by an =, in which case
  1356. it means to define a new type according to the text that
  1357. follows the =. */
  1358. static
  1359. struct type *
  1360. read_type (pp)
  1361. register char **pp;
  1362. {
  1363. register struct type *type = 0;
  1364. register int n;
  1365. struct type *type1;
  1366. int typenums[2];
  1367. int xtypenums[2];
  1368. read_type_number (pp, typenums);
  1369. /* Detect random reference to type not yet defined.
  1370. Allocate a type object but leave it zeroed. */
  1371. if (**pp != '=')
  1372. return dbx_alloc_type (typenums);
  1373. *pp += 2;
  1374. switch ((*pp)[-1])
  1375. {
  1376. case 'x':
  1377. /* Skip the name the cross-ref points to. */
  1378. *pp = (char *) index (*pp, ',');
  1379. /* Just allocate the type and leave it zero if nothing known */
  1380. return dbx_alloc_type (typenums);
  1381. case '0':
  1382. case '1':
  1383. case '2':
  1384. case '3':
  1385. case '4':
  1386. case '5':
  1387. case '6':
  1388. case '7':
  1389. case '8':
  1390. case '9':
  1391. case '(':
  1392. (*pp)--;
  1393. read_type_number (pp, xtypenums);
  1394. type = *dbx_lookup_type (xtypenums);
  1395. if (type == 0)
  1396. type = builtin_type_void;
  1397. *dbx_lookup_type (typenums) = type;
  1398. break;
  1399. case '*':
  1400. type = dbx_alloc_type (typenums);
  1401. smash_to_pointer_type (type, read_type (pp));
  1402. break;
  1403. case 'f':
  1404. type = dbx_alloc_type (typenums);
  1405. smash_to_function_type (type, read_type (pp));
  1406. break;
  1407. case 'r':
  1408. type = read_range_type (pp);
  1409. *dbx_lookup_type (typenums) = type;
  1410. break;
  1411. case 'e':
  1412. type = read_enum_type (pp);
  1413. *dbx_lookup_type (typenums) = type;
  1414. break;
  1415. case 's':
  1416. type = dbx_alloc_type (typenums);
  1417. type = read_struct_type (pp, type);
  1418. break;
  1419. case 'u':
  1420. type = dbx_alloc_type (typenums);
  1421. type = read_struct_type (pp, type);
  1422. TYPE_CODE (type) = TYPE_CODE_UNION;
  1423. break;
  1424. case 'a':
  1425. /* Define an array type. */
  1426. type = dbx_alloc_type (typenums);
  1427. /* dbx expresses array types in terms of a range type for the index,
  1428. and that range type is specified right inside the array type spec
  1429. making ar1;MIN;MAX;VALTYPE */
  1430. if (!strncmp (*pp, "r1;0;", 5))
  1431. (*pp) += 5;
  1432. else if (!strncmp (*pp, "r(0,1);0;", 9))
  1433. (*pp) += 9;
  1434. else break;
  1435. TYPE_CODE (type) = TYPE_CODE_ARRAY;
  1436. n = read_number (pp, ';') + 1;
  1437. TYPE_TARGET_TYPE (type) = read_type (pp);
  1438. TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type)) * n;
  1439. break;
  1440. default:
  1441. error ("Invalid symbol data: unrecognized type-code `%c' at symtab pos %d.",
  1442. (*pp)[-1], symnum);
  1443. }
  1444. if (type == 0)
  1445. abort ();
  1446. #if 0
  1447. /* If this is an overriding temporary alteration for a header file's
  1448. contents, and this type number is unknown in the global definition,
  1449. put this type into the global definition at this type number. */
  1450. if (header_file_prev_index >= 0)
  1451. {
  1452. register struct type **tp
  1453. = explicit_lookup_type (header_file_prev_index, typenums[1]);
  1454. if (*tp == 0)
  1455. *tp = type;
  1456. }
  1457. #endif
  1458. return type;
  1459. }
  1460. /* This page contains subroutines of read_type. */
  1461. /* Read the description of a structure (or union type)
  1462. and return an object describing the type. */
  1463. static struct type *
  1464. read_struct_type (pp, type)
  1465. char **pp;
  1466. register struct type *type;
  1467. {
  1468. struct nextfield
  1469. {
  1470. struct nextfield *next;
  1471. struct field field;
  1472. };
  1473. register struct nextfield *list = 0;
  1474. struct nextfield *new;
  1475. int totalsize;
  1476. char *name;
  1477. register char *p;
  1478. int nfields = 0;
  1479. register int n;
  1480. TYPE_CODE (type) = TYPE_CODE_STRUCT;
  1481. /* First comes the total size in bytes. */
  1482. TYPE_LENGTH (type) = read_number (pp, 0);
  1483. /* Now come the fields, as NAME:TYPENUM,BITPOS,BITSIZE; for each one.
  1484. At the end, we see a semicolon instead of a field. */
  1485. while (**pp != ';')
  1486. {
  1487. /* Check for and handle cretinous dbx symbol name continuation! */
  1488. if (**pp == '\\')
  1489. *pp = next_symbol_text ();
  1490. /* Get space to record the next field's data. */
  1491. new = (struct nextfield *) alloca (sizeof (struct nextfield));
  1492. new->next = list;
  1493. list = new;
  1494. /* Read the data. */
  1495. p = *pp;
  1496. while (*p != ':') p++;
  1497. list->field.name = savestring (*pp, p - *pp);
  1498. *pp = p + 1;
  1499. list->field.type = read_type (pp);
  1500. if (**pp != ',')
  1501. error ("Invalid symbol data: bad structure-type format at symtab pos %d.",
  1502. symnum);
  1503. (*pp)++; /* Skip the comma. */
  1504. list->field.bitpos = read_number (pp, ',');
  1505. list->field.bitsize = read_number (pp, ';');
  1506. /* Detect an unpacked field and mark it as such.
  1507. dbx gives a bit size for all fields.
  1508. If field type is still undefined, that means
  1509. it is going to be a pointer, so consider it unpacked. */
  1510. if ((list->field.bitsize == 8 * TYPE_LENGTH (list->field.type)
  1511. || TYPE_CODE (list->field.type) == TYPE_CODE_UNDEF)
  1512. &&
  1513. list->field.bitpos % 8 == 0)
  1514. list->field.bitsize = 0;
  1515. nfields++;
  1516. }
  1517. (*pp)++; /* Skip the terminating ';'. */
  1518. /* Now create the vector of fields, and record how big it is. */
  1519. TYPE_NFIELDS (type) = nfields;
  1520. TYPE_FIELDS (type) = (struct field *) obstack_alloc (symbol_obstack,
  1521. sizeof (struct field) * nfields);
  1522. /* Copy the saved-up fields into the field vector. */
  1523. for (n = nfields; list; list = list->next)
  1524. TYPE_FIELD (type, --n) = list->field;
  1525. return type;
  1526. }
  1527. /* Read a definition of an enumeration type,
  1528. and create and return a suitable type object.
  1529. Also defines the symbols that represent the values of the type. */
  1530. static struct type *
  1531. read_enum_type (pp)
  1532. register char **pp;
  1533. {
  1534. register char *p;
  1535. register struct type *type;
  1536. char *name;
  1537. register long n;
  1538. register struct symbol *sym;
  1539. int nsyms = 0;
  1540. struct pending **symlist;
  1541. struct pending *osyms, *syms;
  1542. if (within_function)
  1543. symlist = &local_symbols;
  1544. else
  1545. symlist = &file_symbols;
  1546. osyms = *symlist;
  1547. /* Read the value-names and their values.
  1548. The input syntax is NAME:VALUE,NAME:VALUE, and so on.
  1549. A semicolon instead of a NAME means the end. */
  1550. while (**pp && **pp != ';')
  1551. {
  1552. /* Check for and handle cretinous dbx symbol name continuation! */
  1553. if (**pp == '\\')
  1554. *pp = next_symbol_text ();
  1555. p = *pp;
  1556. while (*p != ':') p++;
  1557. name = savestring (*pp, p - *pp);
  1558. *pp = p + 1;
  1559. n = read_number (pp, ',');
  1560. sym = (struct symbol *) xmalloc (sizeof (struct symbol));
  1561. bzero (sym, sizeof (struct symbol));
  1562. SYMBOL_NAME (sym) = name;
  1563. SYMBOL_CLASS (sym) = LOC_CONST;
  1564. SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  1565. SYMBOL_VALUE (sym) = n;
  1566. add_symbol_to_list (sym, symlist);
  1567. nsyms++;
  1568. }
  1569. (*pp)++; /* Skip the semicolon. */
  1570. /* Now create the type and a vector of fields bit enough
  1571. to hold all the value symbols just made. */
  1572. type = (struct type *) obstack_alloc (symbol_obstack, sizeof (struct type));
  1573. bzero (type, sizeof (struct type));
  1574. TYPE_LENGTH (type) = sizeof (int);
  1575. TYPE_CODE (type) = TYPE_CODE_ENUM;
  1576. TYPE_NFIELDS (type) = nsyms;
  1577. TYPE_FIELDS (type) = (struct field *) obstack_alloc (symbol_obstack, sizeof (struct field) * nsyms);
  1578. /* Find the symbols for the values and put them into the type.
  1579. The symbols can be found in the symlist that we put them on
  1580. to cause them to be defined. osyms contains the old value
  1581. of that symlist; everything up to there was defined by us. */
  1582. for (syms = *symlist, n = nsyms; syms != osyms; syms = syms->next)
  1583. {
  1584. SYMBOL_TYPE (syms->symbol) = type;
  1585. TYPE_FIELD_NAME (type, --n) = SYMBOL_NAME (syms->symbol);
  1586. TYPE_FIELD_VALUE (type, n) = SYMBOL_VALUE (syms->symbol);
  1587. TYPE_FIELD_BITPOS (type, n) = 0;
  1588. TYPE_FIELD_BITSIZE (type, n) = 0;
  1589. }
  1590. return type;
  1591. }
  1592. static struct type *
  1593. read_range_type (pp)
  1594. char **pp;
  1595. {
  1596. char *errp = *pp;
  1597. int n1, n2, n3;
  1598. if (**pp == '(')
  1599. {
  1600. /* On a Sun, the first number we really want
  1601. is preceded by "(0," and followed by ")". */
  1602. (*pp)++;
  1603. read_number (pp, ',');
  1604. n1 = read_number (pp, ')');
  1605. (*pp)++;
  1606. }
  1607. else
  1608. n1 = read_number (pp, ';');
  1609. n2 = read_number (pp, ';');
  1610. n3 = read_number (pp, ';');
  1611. n3 ++; /* Change upper-bound to exclusive. */
  1612. if (n2 == 0 && n3 == 0)
  1613. {
  1614. if (sizeof (int) == sizeof (long))
  1615. return builtin_type_unsigned_int;
  1616. else
  1617. return builtin_type_unsigned_long;
  1618. }
  1619. /* This special case is used to represent floating types. */
  1620. if (n3 == 1 && n2 > 0)
  1621. {
  1622. if (n2 == sizeof (float))
  1623. return builtin_type_float;
  1624. if (n2 == sizeof (double))
  1625. return builtin_type_double;
  1626. }
  1627. /* As far as I can tell, n1 is always 1
  1628. except for `char'. Also, for `char', n2 is given as 0
  1629. rather than -128 which is what I would like.
  1630. So use n1 != 0 to prevent char from being taken as unsigned. */
  1631. else if (n2 == 0 && n1 == 1)
  1632. {
  1633. /* an unsigned type */
  1634. if (n3 == (1 << (8 * sizeof (int))))
  1635. return builtin_type_unsigned_int;
  1636. if (n3 == (1 << (8 * sizeof (short))))
  1637. return builtin_type_unsigned_short;
  1638. if (n3 == (1 << (8 * sizeof (char))))
  1639. return builtin_type_unsigned_char;
  1640. }
  1641. else
  1642. {
  1643. /* a signed type */
  1644. if (n3 == (1 << (8 * sizeof (int) - 1)))
  1645. return builtin_type_int;
  1646. if (n3 == (1 << (8 * sizeof (long) - 1)))
  1647. return builtin_type_long;
  1648. if (n3 == (1 << (8 * sizeof (short) - 1)))
  1649. return builtin_type_short;
  1650. if (n3 == (1 << (8 * sizeof (char) - 1)))
  1651. return builtin_type_char;
  1652. }
  1653. error ("Invalid symbol data: range type spec %s at symtab pos %d.",
  1654. errp - 1, symnum);
  1655. }
  1656. /* Read a number from the string pointed to by *PP.
  1657. The value of *PP is advanced over the number.
  1658. If END is nonzero, the character that ends the
  1659. number must match END, or an error happens;
  1660. and that character is skipped if it does match.
  1661. If END is zero, *PP is left pointing to that character. */
  1662. static long
  1663. read_number (pp, end)
  1664. char **pp;
  1665. int end;
  1666. {
  1667. register char *p = *pp;
  1668. register long n = 0;
  1669. register int c;
  1670. int sign = 1;
  1671. /* Handle an optional leading minus sign. */
  1672. if (*p == '-')
  1673. {
  1674. sign = -1;
  1675. p++;
  1676. }
  1677. /* Read the digits, as far as they go. */
  1678. while ((c = *p++) >= '0' && c <= '9')
  1679. {
  1680. n *= 10;
  1681. n += c - '0';
  1682. }
  1683. if (end)
  1684. {
  1685. if (c != end)
  1686. error ("Invalid symbol data: invalid character \\%03o at symbol pos %d.", c, symnum);
  1687. }
  1688. else
  1689. --p;
  1690. *pp = p;
  1691. return n * sign;
  1692. }
  1693. static
  1694. initialize ()
  1695. {
  1696. symfile = 0;
  1697. add_com ("symbol-file", class_files, symbol_file_command,
  1698. "Load symbol table (in dbx format) from executable file FILE.");
  1699. }
  1700. END_FILE