syms.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  1. /* Generic symbol-table support for the BFD library.
  2. Copyright (C) 1990-2015 Free Software Foundation, Inc.
  3. Written by Cygnus Support.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. /*
  18. SECTION
  19. Symbols
  20. BFD tries to maintain as much symbol information as it can when
  21. it moves information from file to file. BFD passes information
  22. to applications though the <<asymbol>> structure. When the
  23. application requests the symbol table, BFD reads the table in
  24. the native form and translates parts of it into the internal
  25. format. To maintain more than the information passed to
  26. applications, some targets keep some information ``behind the
  27. scenes'' in a structure only the particular back end knows
  28. about. For example, the coff back end keeps the original
  29. symbol table structure as well as the canonical structure when
  30. a BFD is read in. On output, the coff back end can reconstruct
  31. the output symbol table so that no information is lost, even
  32. information unique to coff which BFD doesn't know or
  33. understand. If a coff symbol table were read, but were written
  34. through an a.out back end, all the coff specific information
  35. would be lost. The symbol table of a BFD
  36. is not necessarily read in until a canonicalize request is
  37. made. Then the BFD back end fills in a table provided by the
  38. application with pointers to the canonical information. To
  39. output symbols, the application provides BFD with a table of
  40. pointers to pointers to <<asymbol>>s. This allows applications
  41. like the linker to output a symbol as it was read, since the ``behind
  42. the scenes'' information will be still available.
  43. @menu
  44. @* Reading Symbols::
  45. @* Writing Symbols::
  46. @* Mini Symbols::
  47. @* typedef asymbol::
  48. @* symbol handling functions::
  49. @end menu
  50. INODE
  51. Reading Symbols, Writing Symbols, Symbols, Symbols
  52. SUBSECTION
  53. Reading symbols
  54. There are two stages to reading a symbol table from a BFD:
  55. allocating storage, and the actual reading process. This is an
  56. excerpt from an application which reads the symbol table:
  57. | long storage_needed;
  58. | asymbol **symbol_table;
  59. | long number_of_symbols;
  60. | long i;
  61. |
  62. | storage_needed = bfd_get_symtab_upper_bound (abfd);
  63. |
  64. | if (storage_needed < 0)
  65. | FAIL
  66. |
  67. | if (storage_needed == 0)
  68. | return;
  69. |
  70. | symbol_table = xmalloc (storage_needed);
  71. | ...
  72. | number_of_symbols =
  73. | bfd_canonicalize_symtab (abfd, symbol_table);
  74. |
  75. | if (number_of_symbols < 0)
  76. | FAIL
  77. |
  78. | for (i = 0; i < number_of_symbols; i++)
  79. | process_symbol (symbol_table[i]);
  80. All storage for the symbols themselves is in an objalloc
  81. connected to the BFD; it is freed when the BFD is closed.
  82. INODE
  83. Writing Symbols, Mini Symbols, Reading Symbols, Symbols
  84. SUBSECTION
  85. Writing symbols
  86. Writing of a symbol table is automatic when a BFD open for
  87. writing is closed. The application attaches a vector of
  88. pointers to pointers to symbols to the BFD being written, and
  89. fills in the symbol count. The close and cleanup code reads
  90. through the table provided and performs all the necessary
  91. operations. The BFD output code must always be provided with an
  92. ``owned'' symbol: one which has come from another BFD, or one
  93. which has been created using <<bfd_make_empty_symbol>>. Here is an
  94. example showing the creation of a symbol table with only one element:
  95. | #include "sysdep.h"
  96. | #include "bfd.h"
  97. | int main (void)
  98. | {
  99. | bfd *abfd;
  100. | asymbol *ptrs[2];
  101. | asymbol *new;
  102. |
  103. | abfd = bfd_openw ("foo","a.out-sunos-big");
  104. | bfd_set_format (abfd, bfd_object);
  105. | new = bfd_make_empty_symbol (abfd);
  106. | new->name = "dummy_symbol";
  107. | new->section = bfd_make_section_old_way (abfd, ".text");
  108. | new->flags = BSF_GLOBAL;
  109. | new->value = 0x12345;
  110. |
  111. | ptrs[0] = new;
  112. | ptrs[1] = 0;
  113. |
  114. | bfd_set_symtab (abfd, ptrs, 1);
  115. | bfd_close (abfd);
  116. | return 0;
  117. | }
  118. |
  119. | ./makesym
  120. | nm foo
  121. | 00012345 A dummy_symbol
  122. Many formats cannot represent arbitrary symbol information; for
  123. instance, the <<a.out>> object format does not allow an
  124. arbitrary number of sections. A symbol pointing to a section
  125. which is not one of <<.text>>, <<.data>> or <<.bss>> cannot
  126. be described.
  127. INODE
  128. Mini Symbols, typedef asymbol, Writing Symbols, Symbols
  129. SUBSECTION
  130. Mini Symbols
  131. Mini symbols provide read-only access to the symbol table.
  132. They use less memory space, but require more time to access.
  133. They can be useful for tools like nm or objdump, which may
  134. have to handle symbol tables of extremely large executables.
  135. The <<bfd_read_minisymbols>> function will read the symbols
  136. into memory in an internal form. It will return a <<void *>>
  137. pointer to a block of memory, a symbol count, and the size of
  138. each symbol. The pointer is allocated using <<malloc>>, and
  139. should be freed by the caller when it is no longer needed.
  140. The function <<bfd_minisymbol_to_symbol>> will take a pointer
  141. to a minisymbol, and a pointer to a structure returned by
  142. <<bfd_make_empty_symbol>>, and return a <<asymbol>> structure.
  143. The return value may or may not be the same as the value from
  144. <<bfd_make_empty_symbol>> which was passed in.
  145. */
  146. /*
  147. DOCDD
  148. INODE
  149. typedef asymbol, symbol handling functions, Mini Symbols, Symbols
  150. */
  151. /*
  152. SUBSECTION
  153. typedef asymbol
  154. An <<asymbol>> has the form:
  155. */
  156. /*
  157. CODE_FRAGMENT
  158. .
  159. .typedef struct bfd_symbol
  160. .{
  161. . {* A pointer to the BFD which owns the symbol. This information
  162. . is necessary so that a back end can work out what additional
  163. . information (invisible to the application writer) is carried
  164. . with the symbol.
  165. .
  166. . This field is *almost* redundant, since you can use section->owner
  167. . instead, except that some symbols point to the global sections
  168. . bfd_{abs,com,und}_section. This could be fixed by making
  169. . these globals be per-bfd (or per-target-flavor). FIXME. *}
  170. . struct bfd *the_bfd; {* Use bfd_asymbol_bfd(sym) to access this field. *}
  171. .
  172. . {* The text of the symbol. The name is left alone, and not copied; the
  173. . application may not alter it. *}
  174. . const char *name;
  175. .
  176. . {* The value of the symbol. This really should be a union of a
  177. . numeric value with a pointer, since some flags indicate that
  178. . a pointer to another symbol is stored here. *}
  179. . symvalue value;
  180. .
  181. . {* Attributes of a symbol. *}
  182. .#define BSF_NO_FLAGS 0x00
  183. .
  184. . {* The symbol has local scope; <<static>> in <<C>>. The value
  185. . is the offset into the section of the data. *}
  186. .#define BSF_LOCAL (1 << 0)
  187. .
  188. . {* The symbol has global scope; initialized data in <<C>>. The
  189. . value is the offset into the section of the data. *}
  190. .#define BSF_GLOBAL (1 << 1)
  191. .
  192. . {* The symbol has global scope and is exported. The value is
  193. . the offset into the section of the data. *}
  194. .#define BSF_EXPORT BSF_GLOBAL {* No real difference. *}
  195. .
  196. . {* A normal C symbol would be one of:
  197. . <<BSF_LOCAL>>, <<BSF_COMMON>>, <<BSF_UNDEFINED>> or
  198. . <<BSF_GLOBAL>>. *}
  199. .
  200. . {* The symbol is a debugging record. The value has an arbitrary
  201. . meaning, unless BSF_DEBUGGING_RELOC is also set. *}
  202. .#define BSF_DEBUGGING (1 << 2)
  203. .
  204. . {* The symbol denotes a function entry point. Used in ELF,
  205. . perhaps others someday. *}
  206. .#define BSF_FUNCTION (1 << 3)
  207. .
  208. . {* Used by the linker. *}
  209. .#define BSF_KEEP (1 << 5)
  210. .#define BSF_KEEP_G (1 << 6)
  211. .
  212. . {* A weak global symbol, overridable without warnings by
  213. . a regular global symbol of the same name. *}
  214. .#define BSF_WEAK (1 << 7)
  215. .
  216. . {* This symbol was created to point to a section, e.g. ELF's
  217. . STT_SECTION symbols. *}
  218. .#define BSF_SECTION_SYM (1 << 8)
  219. .
  220. . {* The symbol used to be a common symbol, but now it is
  221. . allocated. *}
  222. .#define BSF_OLD_COMMON (1 << 9)
  223. .
  224. . {* In some files the type of a symbol sometimes alters its
  225. . location in an output file - ie in coff a <<ISFCN>> symbol
  226. . which is also <<C_EXT>> symbol appears where it was
  227. . declared and not at the end of a section. This bit is set
  228. . by the target BFD part to convey this information. *}
  229. .#define BSF_NOT_AT_END (1 << 10)
  230. .
  231. . {* Signal that the symbol is the label of constructor section. *}
  232. .#define BSF_CONSTRUCTOR (1 << 11)
  233. .
  234. . {* Signal that the symbol is a warning symbol. The name is a
  235. . warning. The name of the next symbol is the one to warn about;
  236. . if a reference is made to a symbol with the same name as the next
  237. . symbol, a warning is issued by the linker. *}
  238. .#define BSF_WARNING (1 << 12)
  239. .
  240. . {* Signal that the symbol is indirect. This symbol is an indirect
  241. . pointer to the symbol with the same name as the next symbol. *}
  242. .#define BSF_INDIRECT (1 << 13)
  243. .
  244. . {* BSF_FILE marks symbols that contain a file name. This is used
  245. . for ELF STT_FILE symbols. *}
  246. .#define BSF_FILE (1 << 14)
  247. .
  248. . {* Symbol is from dynamic linking information. *}
  249. .#define BSF_DYNAMIC (1 << 15)
  250. .
  251. . {* The symbol denotes a data object. Used in ELF, and perhaps
  252. . others someday. *}
  253. .#define BSF_OBJECT (1 << 16)
  254. .
  255. . {* This symbol is a debugging symbol. The value is the offset
  256. . into the section of the data. BSF_DEBUGGING should be set
  257. . as well. *}
  258. .#define BSF_DEBUGGING_RELOC (1 << 17)
  259. .
  260. . {* This symbol is thread local. Used in ELF. *}
  261. .#define BSF_THREAD_LOCAL (1 << 18)
  262. .
  263. . {* This symbol represents a complex relocation expression,
  264. . with the expression tree serialized in the symbol name. *}
  265. .#define BSF_RELC (1 << 19)
  266. .
  267. . {* This symbol represents a signed complex relocation expression,
  268. . with the expression tree serialized in the symbol name. *}
  269. .#define BSF_SRELC (1 << 20)
  270. .
  271. . {* This symbol was created by bfd_get_synthetic_symtab. *}
  272. .#define BSF_SYNTHETIC (1 << 21)
  273. .
  274. . {* This symbol is an indirect code object. Unrelated to BSF_INDIRECT.
  275. . The dynamic linker will compute the value of this symbol by
  276. . calling the function that it points to. BSF_FUNCTION must
  277. . also be also set. *}
  278. .#define BSF_GNU_INDIRECT_FUNCTION (1 << 22)
  279. . {* This symbol is a globally unique data object. The dynamic linker
  280. . will make sure that in the entire process there is just one symbol
  281. . with this name and type in use. BSF_OBJECT must also be set. *}
  282. .#define BSF_GNU_UNIQUE (1 << 23)
  283. .
  284. . flagword flags;
  285. .
  286. . {* A pointer to the section to which this symbol is
  287. . relative. This will always be non NULL, there are special
  288. . sections for undefined and absolute symbols. *}
  289. . struct bfd_section *section;
  290. .
  291. . {* Back end special data. *}
  292. . union
  293. . {
  294. . void *p;
  295. . bfd_vma i;
  296. . }
  297. . udata;
  298. .}
  299. .asymbol;
  300. .
  301. */
  302. #include "sysdep.h"
  303. #include "bfd.h"
  304. #include "libbfd.h"
  305. #include "safe-ctype.h"
  306. #include "bfdlink.h"
  307. #include "aout/stab_gnu.h"
  308. /*
  309. DOCDD
  310. INODE
  311. symbol handling functions, , typedef asymbol, Symbols
  312. SUBSECTION
  313. Symbol handling functions
  314. */
  315. /*
  316. FUNCTION
  317. bfd_get_symtab_upper_bound
  318. DESCRIPTION
  319. Return the number of bytes required to store a vector of pointers
  320. to <<asymbols>> for all the symbols in the BFD @var{abfd},
  321. including a terminal NULL pointer. If there are no symbols in
  322. the BFD, then return 0. If an error occurs, return -1.
  323. .#define bfd_get_symtab_upper_bound(abfd) \
  324. . BFD_SEND (abfd, _bfd_get_symtab_upper_bound, (abfd))
  325. .
  326. */
  327. /*
  328. FUNCTION
  329. bfd_is_local_label
  330. SYNOPSIS
  331. bfd_boolean bfd_is_local_label (bfd *abfd, asymbol *sym);
  332. DESCRIPTION
  333. Return TRUE if the given symbol @var{sym} in the BFD @var{abfd} is
  334. a compiler generated local label, else return FALSE.
  335. */
  336. bfd_boolean
  337. bfd_is_local_label (bfd *abfd, asymbol *sym)
  338. {
  339. /* The BSF_SECTION_SYM check is needed for IA-64, where every label that
  340. starts with '.' is local. This would accidentally catch section names
  341. if we didn't reject them here. */
  342. if ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_FILE | BSF_SECTION_SYM)) != 0)
  343. return FALSE;
  344. if (sym->name == NULL)
  345. return FALSE;
  346. return bfd_is_local_label_name (abfd, sym->name);
  347. }
  348. /*
  349. FUNCTION
  350. bfd_is_local_label_name
  351. SYNOPSIS
  352. bfd_boolean bfd_is_local_label_name (bfd *abfd, const char *name);
  353. DESCRIPTION
  354. Return TRUE if a symbol with the name @var{name} in the BFD
  355. @var{abfd} is a compiler generated local label, else return
  356. FALSE. This just checks whether the name has the form of a
  357. local label.
  358. .#define bfd_is_local_label_name(abfd, name) \
  359. . BFD_SEND (abfd, _bfd_is_local_label_name, (abfd, name))
  360. .
  361. */
  362. /*
  363. FUNCTION
  364. bfd_is_target_special_symbol
  365. SYNOPSIS
  366. bfd_boolean bfd_is_target_special_symbol (bfd *abfd, asymbol *sym);
  367. DESCRIPTION
  368. Return TRUE iff a symbol @var{sym} in the BFD @var{abfd} is something
  369. special to the particular target represented by the BFD. Such symbols
  370. should normally not be mentioned to the user.
  371. .#define bfd_is_target_special_symbol(abfd, sym) \
  372. . BFD_SEND (abfd, _bfd_is_target_special_symbol, (abfd, sym))
  373. .
  374. */
  375. /*
  376. FUNCTION
  377. bfd_canonicalize_symtab
  378. DESCRIPTION
  379. Read the symbols from the BFD @var{abfd}, and fills in
  380. the vector @var{location} with pointers to the symbols and
  381. a trailing NULL.
  382. Return the actual number of symbol pointers, not
  383. including the NULL.
  384. .#define bfd_canonicalize_symtab(abfd, location) \
  385. . BFD_SEND (abfd, _bfd_canonicalize_symtab, (abfd, location))
  386. .
  387. */
  388. /*
  389. FUNCTION
  390. bfd_set_symtab
  391. SYNOPSIS
  392. bfd_boolean bfd_set_symtab
  393. (bfd *abfd, asymbol **location, unsigned int count);
  394. DESCRIPTION
  395. Arrange that when the output BFD @var{abfd} is closed,
  396. the table @var{location} of @var{count} pointers to symbols
  397. will be written.
  398. */
  399. bfd_boolean
  400. bfd_set_symtab (bfd *abfd, asymbol **location, unsigned int symcount)
  401. {
  402. if (abfd->format != bfd_object || bfd_read_p (abfd))
  403. {
  404. bfd_set_error (bfd_error_invalid_operation);
  405. return FALSE;
  406. }
  407. bfd_get_outsymbols (abfd) = location;
  408. bfd_get_symcount (abfd) = symcount;
  409. return TRUE;
  410. }
  411. /*
  412. FUNCTION
  413. bfd_print_symbol_vandf
  414. SYNOPSIS
  415. void bfd_print_symbol_vandf (bfd *abfd, void *file, asymbol *symbol);
  416. DESCRIPTION
  417. Print the value and flags of the @var{symbol} supplied to the
  418. stream @var{file}.
  419. */
  420. void
  421. bfd_print_symbol_vandf (bfd *abfd, void *arg, asymbol *symbol)
  422. {
  423. FILE *file = (FILE *) arg;
  424. flagword type = symbol->flags;
  425. if (symbol->section != NULL)
  426. bfd_fprintf_vma (abfd, file, symbol->value + symbol->section->vma);
  427. else
  428. bfd_fprintf_vma (abfd, file, symbol->value);
  429. /* This presumes that a symbol can not be both BSF_DEBUGGING and
  430. BSF_DYNAMIC, nor more than one of BSF_FUNCTION, BSF_FILE, and
  431. BSF_OBJECT. */
  432. fprintf (file, " %c%c%c%c%c%c%c",
  433. ((type & BSF_LOCAL)
  434. ? (type & BSF_GLOBAL) ? '!' : 'l'
  435. : (type & BSF_GLOBAL) ? 'g'
  436. : (type & BSF_GNU_UNIQUE) ? 'u' : ' '),
  437. (type & BSF_WEAK) ? 'w' : ' ',
  438. (type & BSF_CONSTRUCTOR) ? 'C' : ' ',
  439. (type & BSF_WARNING) ? 'W' : ' ',
  440. (type & BSF_INDIRECT) ? 'I' : (type & BSF_GNU_INDIRECT_FUNCTION) ? 'i' : ' ',
  441. (type & BSF_DEBUGGING) ? 'd' : (type & BSF_DYNAMIC) ? 'D' : ' ',
  442. ((type & BSF_FUNCTION)
  443. ? 'F'
  444. : ((type & BSF_FILE)
  445. ? 'f'
  446. : ((type & BSF_OBJECT) ? 'O' : ' '))));
  447. }
  448. /*
  449. FUNCTION
  450. bfd_make_empty_symbol
  451. DESCRIPTION
  452. Create a new <<asymbol>> structure for the BFD @var{abfd}
  453. and return a pointer to it.
  454. This routine is necessary because each back end has private
  455. information surrounding the <<asymbol>>. Building your own
  456. <<asymbol>> and pointing to it will not create the private
  457. information, and will cause problems later on.
  458. .#define bfd_make_empty_symbol(abfd) \
  459. . BFD_SEND (abfd, _bfd_make_empty_symbol, (abfd))
  460. .
  461. */
  462. /*
  463. FUNCTION
  464. _bfd_generic_make_empty_symbol
  465. SYNOPSIS
  466. asymbol *_bfd_generic_make_empty_symbol (bfd *);
  467. DESCRIPTION
  468. Create a new <<asymbol>> structure for the BFD @var{abfd}
  469. and return a pointer to it. Used by core file routines,
  470. binary back-end and anywhere else where no private info
  471. is needed.
  472. */
  473. asymbol *
  474. _bfd_generic_make_empty_symbol (bfd *abfd)
  475. {
  476. bfd_size_type amt = sizeof (asymbol);
  477. asymbol *new_symbol = (asymbol *) bfd_zalloc (abfd, amt);
  478. if (new_symbol)
  479. new_symbol->the_bfd = abfd;
  480. return new_symbol;
  481. }
  482. /*
  483. FUNCTION
  484. bfd_make_debug_symbol
  485. DESCRIPTION
  486. Create a new <<asymbol>> structure for the BFD @var{abfd},
  487. to be used as a debugging symbol. Further details of its use have
  488. yet to be worked out.
  489. .#define bfd_make_debug_symbol(abfd,ptr,size) \
  490. . BFD_SEND (abfd, _bfd_make_debug_symbol, (abfd, ptr, size))
  491. .
  492. */
  493. struct section_to_type
  494. {
  495. const char *section;
  496. char type;
  497. };
  498. /* Map section names to POSIX/BSD single-character symbol types.
  499. This table is probably incomplete. It is sorted for convenience of
  500. adding entries. Since it is so short, a linear search is used. */
  501. static const struct section_to_type stt[] =
  502. {
  503. {".bss", 'b'},
  504. {"code", 't'}, /* MRI .text */
  505. {".data", 'd'},
  506. {"*DEBUG*", 'N'},
  507. {".debug", 'N'}, /* MSVC's .debug (non-standard debug syms) */
  508. {".drectve", 'i'}, /* MSVC's .drective section */
  509. {".edata", 'e'}, /* MSVC's .edata (export) section */
  510. {".fini", 't'}, /* ELF fini section */
  511. {".idata", 'i'}, /* MSVC's .idata (import) section */
  512. {".init", 't'}, /* ELF init section */
  513. {".pdata", 'p'}, /* MSVC's .pdata (stack unwind) section */
  514. {".rdata", 'r'}, /* Read only data. */
  515. {".rodata", 'r'}, /* Read only data. */
  516. {".sbss", 's'}, /* Small BSS (uninitialized data). */
  517. {".scommon", 'c'}, /* Small common. */
  518. {".sdata", 'g'}, /* Small initialized data. */
  519. {".text", 't'},
  520. {"vars", 'd'}, /* MRI .data */
  521. {"zerovars", 'b'}, /* MRI .bss */
  522. {0, 0}
  523. };
  524. /* Return the single-character symbol type corresponding to
  525. section S, or '?' for an unknown COFF section.
  526. Check for any leading string which matches, so .text5 returns
  527. 't' as well as .text */
  528. static char
  529. coff_section_type (const char *s)
  530. {
  531. const struct section_to_type *t;
  532. for (t = &stt[0]; t->section; t++)
  533. if (!strncmp (s, t->section, strlen (t->section)))
  534. return t->type;
  535. return '?';
  536. }
  537. /* Return the single-character symbol type corresponding to section
  538. SECTION, or '?' for an unknown section. This uses section flags to
  539. identify sections.
  540. FIXME These types are unhandled: c, i, e, p. If we handled these also,
  541. we could perhaps obsolete coff_section_type. */
  542. static char
  543. decode_section_type (const struct bfd_section *section)
  544. {
  545. if (section->flags & SEC_CODE)
  546. return 't';
  547. if (section->flags & SEC_DATA)
  548. {
  549. if (section->flags & SEC_READONLY)
  550. return 'r';
  551. else if (section->flags & SEC_SMALL_DATA)
  552. return 'g';
  553. else
  554. return 'd';
  555. }
  556. if ((section->flags & SEC_HAS_CONTENTS) == 0)
  557. {
  558. if (section->flags & SEC_SMALL_DATA)
  559. return 's';
  560. else
  561. return 'b';
  562. }
  563. if (section->flags & SEC_DEBUGGING)
  564. return 'N';
  565. if ((section->flags & SEC_HAS_CONTENTS) && (section->flags & SEC_READONLY))
  566. return 'n';
  567. return '?';
  568. }
  569. /*
  570. FUNCTION
  571. bfd_decode_symclass
  572. DESCRIPTION
  573. Return a character corresponding to the symbol
  574. class of @var{symbol}, or '?' for an unknown class.
  575. SYNOPSIS
  576. int bfd_decode_symclass (asymbol *symbol);
  577. */
  578. int
  579. bfd_decode_symclass (asymbol *symbol)
  580. {
  581. char c;
  582. if (symbol->section && bfd_is_com_section (symbol->section))
  583. return 'C';
  584. if (bfd_is_und_section (symbol->section))
  585. {
  586. if (symbol->flags & BSF_WEAK)
  587. {
  588. /* If weak, determine if it's specifically an object
  589. or non-object weak. */
  590. if (symbol->flags & BSF_OBJECT)
  591. return 'v';
  592. else
  593. return 'w';
  594. }
  595. else
  596. return 'U';
  597. }
  598. if (bfd_is_ind_section (symbol->section))
  599. return 'I';
  600. if (symbol->flags & BSF_GNU_INDIRECT_FUNCTION)
  601. return 'i';
  602. if (symbol->flags & BSF_WEAK)
  603. {
  604. /* If weak, determine if it's specifically an object
  605. or non-object weak. */
  606. if (symbol->flags & BSF_OBJECT)
  607. return 'V';
  608. else
  609. return 'W';
  610. }
  611. if (symbol->flags & BSF_GNU_UNIQUE)
  612. return 'u';
  613. if (!(symbol->flags & (BSF_GLOBAL | BSF_LOCAL)))
  614. return '?';
  615. if (bfd_is_abs_section (symbol->section))
  616. c = 'a';
  617. else if (symbol->section)
  618. {
  619. c = coff_section_type (symbol->section->name);
  620. if (c == '?')
  621. c = decode_section_type (symbol->section);
  622. }
  623. else
  624. return '?';
  625. if (symbol->flags & BSF_GLOBAL)
  626. c = TOUPPER (c);
  627. return c;
  628. /* We don't have to handle these cases just yet, but we will soon:
  629. N_SETV: 'v';
  630. N_SETA: 'l';
  631. N_SETT: 'x';
  632. N_SETD: 'z';
  633. N_SETB: 's';
  634. N_INDR: 'i';
  635. */
  636. }
  637. /*
  638. FUNCTION
  639. bfd_is_undefined_symclass
  640. DESCRIPTION
  641. Returns non-zero if the class symbol returned by
  642. bfd_decode_symclass represents an undefined symbol.
  643. Returns zero otherwise.
  644. SYNOPSIS
  645. bfd_boolean bfd_is_undefined_symclass (int symclass);
  646. */
  647. bfd_boolean
  648. bfd_is_undefined_symclass (int symclass)
  649. {
  650. return symclass == 'U' || symclass == 'w' || symclass == 'v';
  651. }
  652. /*
  653. FUNCTION
  654. bfd_symbol_info
  655. DESCRIPTION
  656. Fill in the basic info about symbol that nm needs.
  657. Additional info may be added by the back-ends after
  658. calling this function.
  659. SYNOPSIS
  660. void bfd_symbol_info (asymbol *symbol, symbol_info *ret);
  661. */
  662. void
  663. bfd_symbol_info (asymbol *symbol, symbol_info *ret)
  664. {
  665. ret->type = bfd_decode_symclass (symbol);
  666. if (bfd_is_undefined_symclass (ret->type))
  667. ret->value = 0;
  668. else
  669. ret->value = symbol->value + symbol->section->vma;
  670. ret->name = symbol->name;
  671. }
  672. /*
  673. FUNCTION
  674. bfd_copy_private_symbol_data
  675. SYNOPSIS
  676. bfd_boolean bfd_copy_private_symbol_data
  677. (bfd *ibfd, asymbol *isym, bfd *obfd, asymbol *osym);
  678. DESCRIPTION
  679. Copy private symbol information from @var{isym} in the BFD
  680. @var{ibfd} to the symbol @var{osym} in the BFD @var{obfd}.
  681. Return <<TRUE>> on success, <<FALSE>> on error. Possible error
  682. returns are:
  683. o <<bfd_error_no_memory>> -
  684. Not enough memory exists to create private data for @var{osec}.
  685. .#define bfd_copy_private_symbol_data(ibfd, isymbol, obfd, osymbol) \
  686. . BFD_SEND (obfd, _bfd_copy_private_symbol_data, \
  687. . (ibfd, isymbol, obfd, osymbol))
  688. .
  689. */
  690. /* The generic version of the function which returns mini symbols.
  691. This is used when the backend does not provide a more efficient
  692. version. It just uses BFD asymbol structures as mini symbols. */
  693. long
  694. _bfd_generic_read_minisymbols (bfd *abfd,
  695. bfd_boolean dynamic,
  696. void **minisymsp,
  697. unsigned int *sizep)
  698. {
  699. long storage;
  700. asymbol **syms = NULL;
  701. long symcount;
  702. if (dynamic)
  703. storage = bfd_get_dynamic_symtab_upper_bound (abfd);
  704. else
  705. storage = bfd_get_symtab_upper_bound (abfd);
  706. if (storage < 0)
  707. goto error_return;
  708. if (storage == 0)
  709. return 0;
  710. syms = (asymbol **) bfd_malloc (storage);
  711. if (syms == NULL)
  712. goto error_return;
  713. if (dynamic)
  714. symcount = bfd_canonicalize_dynamic_symtab (abfd, syms);
  715. else
  716. symcount = bfd_canonicalize_symtab (abfd, syms);
  717. if (symcount < 0)
  718. goto error_return;
  719. *minisymsp = syms;
  720. *sizep = sizeof (asymbol *);
  721. return symcount;
  722. error_return:
  723. bfd_set_error (bfd_error_no_symbols);
  724. if (syms != NULL)
  725. free (syms);
  726. return -1;
  727. }
  728. /* The generic version of the function which converts a minisymbol to
  729. an asymbol. We don't worry about the sym argument we are passed;
  730. we just return the asymbol the minisymbol points to. */
  731. asymbol *
  732. _bfd_generic_minisymbol_to_symbol (bfd *abfd ATTRIBUTE_UNUSED,
  733. bfd_boolean dynamic ATTRIBUTE_UNUSED,
  734. const void *minisym,
  735. asymbol *sym ATTRIBUTE_UNUSED)
  736. {
  737. return *(asymbol **) minisym;
  738. }
  739. /* Look through stabs debugging information in .stab and .stabstr
  740. sections to find the source file and line closest to a desired
  741. location. This is used by COFF and ELF targets. It sets *pfound
  742. to TRUE if it finds some information. The *pinfo field is used to
  743. pass cached information in and out of this routine; this first time
  744. the routine is called for a BFD, *pinfo should be NULL. The value
  745. placed in *pinfo should be saved with the BFD, and passed back each
  746. time this function is called. */
  747. /* We use a cache by default. */
  748. #define ENABLE_CACHING
  749. /* We keep an array of indexentry structures to record where in the
  750. stabs section we should look to find line number information for a
  751. particular address. */
  752. struct indexentry
  753. {
  754. bfd_vma val;
  755. bfd_byte *stab;
  756. bfd_byte *str;
  757. char *directory_name;
  758. char *file_name;
  759. char *function_name;
  760. };
  761. /* Compare two indexentry structures. This is called via qsort. */
  762. static int
  763. cmpindexentry (const void *a, const void *b)
  764. {
  765. const struct indexentry *contestantA = (const struct indexentry *) a;
  766. const struct indexentry *contestantB = (const struct indexentry *) b;
  767. if (contestantA->val < contestantB->val)
  768. return -1;
  769. else if (contestantA->val > contestantB->val)
  770. return 1;
  771. else
  772. return 0;
  773. }
  774. /* A pointer to this structure is stored in *pinfo. */
  775. struct stab_find_info
  776. {
  777. /* The .stab section. */
  778. asection *stabsec;
  779. /* The .stabstr section. */
  780. asection *strsec;
  781. /* The contents of the .stab section. */
  782. bfd_byte *stabs;
  783. /* The contents of the .stabstr section. */
  784. bfd_byte *strs;
  785. /* A table that indexes stabs by memory address. */
  786. struct indexentry *indextable;
  787. /* The number of entries in indextable. */
  788. int indextablesize;
  789. #ifdef ENABLE_CACHING
  790. /* Cached values to restart quickly. */
  791. struct indexentry *cached_indexentry;
  792. bfd_vma cached_offset;
  793. bfd_byte *cached_stab;
  794. char *cached_file_name;
  795. #endif
  796. /* Saved ptr to malloc'ed filename. */
  797. char *filename;
  798. };
  799. bfd_boolean
  800. _bfd_stab_section_find_nearest_line (bfd *abfd,
  801. asymbol **symbols,
  802. asection *section,
  803. bfd_vma offset,
  804. bfd_boolean *pfound,
  805. const char **pfilename,
  806. const char **pfnname,
  807. unsigned int *pline,
  808. void **pinfo)
  809. {
  810. struct stab_find_info *info;
  811. bfd_size_type stabsize, strsize;
  812. bfd_byte *stab, *str;
  813. bfd_byte *nul_fun, *nul_str;
  814. bfd_size_type stroff;
  815. struct indexentry *indexentry;
  816. char *file_name;
  817. char *directory_name;
  818. bfd_boolean saw_line, saw_func;
  819. *pfound = FALSE;
  820. *pfilename = bfd_get_filename (abfd);
  821. *pfnname = NULL;
  822. *pline = 0;
  823. /* Stabs entries use a 12 byte format:
  824. 4 byte string table index
  825. 1 byte stab type
  826. 1 byte stab other field
  827. 2 byte stab desc field
  828. 4 byte stab value
  829. FIXME: This will have to change for a 64 bit object format.
  830. The stabs symbols are divided into compilation units. For the
  831. first entry in each unit, the type of 0, the value is the length
  832. of the string table for this unit, and the desc field is the
  833. number of stabs symbols for this unit. */
  834. #define STRDXOFF (0)
  835. #define TYPEOFF (4)
  836. #define OTHEROFF (5)
  837. #define DESCOFF (6)
  838. #define VALOFF (8)
  839. #define STABSIZE (12)
  840. info = (struct stab_find_info *) *pinfo;
  841. if (info != NULL)
  842. {
  843. if (info->stabsec == NULL || info->strsec == NULL)
  844. {
  845. /* No stabs debugging information. */
  846. return TRUE;
  847. }
  848. stabsize = (info->stabsec->rawsize
  849. ? info->stabsec->rawsize
  850. : info->stabsec->size);
  851. strsize = (info->strsec->rawsize
  852. ? info->strsec->rawsize
  853. : info->strsec->size);
  854. }
  855. else
  856. {
  857. long reloc_size, reloc_count;
  858. arelent **reloc_vector;
  859. int i;
  860. char *function_name;
  861. bfd_size_type amt = sizeof *info;
  862. info = (struct stab_find_info *) bfd_zalloc (abfd, amt);
  863. if (info == NULL)
  864. return FALSE;
  865. /* FIXME: When using the linker --split-by-file or
  866. --split-by-reloc options, it is possible for the .stab and
  867. .stabstr sections to be split. We should handle that. */
  868. info->stabsec = bfd_get_section_by_name (abfd, ".stab");
  869. info->strsec = bfd_get_section_by_name (abfd, ".stabstr");
  870. if (info->stabsec == NULL || info->strsec == NULL)
  871. {
  872. /* Try SOM section names. */
  873. info->stabsec = bfd_get_section_by_name (abfd, "$GDB_SYMBOLS$");
  874. info->strsec = bfd_get_section_by_name (abfd, "$GDB_STRINGS$");
  875. if (info->stabsec == NULL || info->strsec == NULL)
  876. {
  877. /* No stabs debugging information. Set *pinfo so that we
  878. can return quickly in the info != NULL case above. */
  879. *pinfo = info;
  880. return TRUE;
  881. }
  882. }
  883. stabsize = (info->stabsec->rawsize
  884. ? info->stabsec->rawsize
  885. : info->stabsec->size);
  886. stabsize = (stabsize / STABSIZE) * STABSIZE;
  887. strsize = (info->strsec->rawsize
  888. ? info->strsec->rawsize
  889. : info->strsec->size);
  890. info->stabs = (bfd_byte *) bfd_alloc (abfd, stabsize);
  891. info->strs = (bfd_byte *) bfd_alloc (abfd, strsize);
  892. if (info->stabs == NULL || info->strs == NULL)
  893. return FALSE;
  894. if (! bfd_get_section_contents (abfd, info->stabsec, info->stabs,
  895. 0, stabsize)
  896. || ! bfd_get_section_contents (abfd, info->strsec, info->strs,
  897. 0, strsize))
  898. return FALSE;
  899. /* If this is a relocatable object file, we have to relocate
  900. the entries in .stab. This should always be simple 32 bit
  901. relocations against symbols defined in this object file, so
  902. this should be no big deal. */
  903. reloc_size = bfd_get_reloc_upper_bound (abfd, info->stabsec);
  904. if (reloc_size < 0)
  905. return FALSE;
  906. reloc_vector = (arelent **) bfd_malloc (reloc_size);
  907. if (reloc_vector == NULL && reloc_size != 0)
  908. return FALSE;
  909. reloc_count = bfd_canonicalize_reloc (abfd, info->stabsec, reloc_vector,
  910. symbols);
  911. if (reloc_count < 0)
  912. {
  913. if (reloc_vector != NULL)
  914. free (reloc_vector);
  915. return FALSE;
  916. }
  917. if (reloc_count > 0)
  918. {
  919. arelent **pr;
  920. for (pr = reloc_vector; *pr != NULL; pr++)
  921. {
  922. arelent *r;
  923. unsigned long val;
  924. asymbol *sym;
  925. r = *pr;
  926. /* Ignore R_*_NONE relocs. */
  927. if (r->howto->dst_mask == 0)
  928. continue;
  929. if (r->howto->rightshift != 0
  930. || r->howto->size != 2
  931. || r->howto->bitsize != 32
  932. || r->howto->pc_relative
  933. || r->howto->bitpos != 0
  934. || r->howto->dst_mask != 0xffffffff)
  935. {
  936. (*_bfd_error_handler)
  937. (_("Unsupported .stab relocation"));
  938. bfd_set_error (bfd_error_invalid_operation);
  939. if (reloc_vector != NULL)
  940. free (reloc_vector);
  941. return FALSE;
  942. }
  943. val = bfd_get_32 (abfd, info->stabs + r->address);
  944. val &= r->howto->src_mask;
  945. sym = *r->sym_ptr_ptr;
  946. val += sym->value + sym->section->vma + r->addend;
  947. bfd_put_32 (abfd, (bfd_vma) val, info->stabs + r->address);
  948. }
  949. }
  950. if (reloc_vector != NULL)
  951. free (reloc_vector);
  952. /* First time through this function, build a table matching
  953. function VM addresses to stabs, then sort based on starting
  954. VM address. Do this in two passes: once to count how many
  955. table entries we'll need, and a second to actually build the
  956. table. */
  957. info->indextablesize = 0;
  958. nul_fun = NULL;
  959. for (stab = info->stabs; stab < info->stabs + stabsize; stab += STABSIZE)
  960. {
  961. if (stab[TYPEOFF] == (bfd_byte) N_SO)
  962. {
  963. /* if we did not see a function def, leave space for one. */
  964. if (nul_fun != NULL)
  965. ++info->indextablesize;
  966. /* N_SO with null name indicates EOF */
  967. if (bfd_get_32 (abfd, stab + STRDXOFF) == 0)
  968. nul_fun = NULL;
  969. else
  970. {
  971. nul_fun = stab;
  972. /* two N_SO's in a row is a filename and directory. Skip */
  973. if (stab + STABSIZE + TYPEOFF < info->stabs + stabsize
  974. && *(stab + STABSIZE + TYPEOFF) == (bfd_byte) N_SO)
  975. stab += STABSIZE;
  976. }
  977. }
  978. else if (stab[TYPEOFF] == (bfd_byte) N_FUN
  979. && bfd_get_32 (abfd, stab + STRDXOFF) != 0)
  980. {
  981. nul_fun = NULL;
  982. ++info->indextablesize;
  983. }
  984. }
  985. if (nul_fun != NULL)
  986. ++info->indextablesize;
  987. if (info->indextablesize == 0)
  988. return TRUE;
  989. ++info->indextablesize;
  990. amt = info->indextablesize;
  991. amt *= sizeof (struct indexentry);
  992. info->indextable = (struct indexentry *) bfd_alloc (abfd, amt);
  993. if (info->indextable == NULL)
  994. return FALSE;
  995. file_name = NULL;
  996. directory_name = NULL;
  997. nul_fun = NULL;
  998. stroff = 0;
  999. for (i = 0, stab = info->stabs, nul_str = str = info->strs;
  1000. i < info->indextablesize && stab < info->stabs + stabsize;
  1001. stab += STABSIZE)
  1002. {
  1003. switch (stab[TYPEOFF])
  1004. {
  1005. case 0:
  1006. /* This is the first entry in a compilation unit. */
  1007. if ((bfd_size_type) ((info->strs + strsize) - str) < stroff)
  1008. break;
  1009. str += stroff;
  1010. stroff = bfd_get_32 (abfd, stab + VALOFF);
  1011. break;
  1012. case N_SO:
  1013. /* The main file name. */
  1014. /* The following code creates a new indextable entry with
  1015. a NULL function name if there were no N_FUNs in a file.
  1016. Note that a N_SO without a file name is an EOF and
  1017. there could be 2 N_SO following it with the new filename
  1018. and directory. */
  1019. if (nul_fun != NULL)
  1020. {
  1021. info->indextable[i].val = bfd_get_32 (abfd, nul_fun + VALOFF);
  1022. info->indextable[i].stab = nul_fun;
  1023. info->indextable[i].str = nul_str;
  1024. info->indextable[i].directory_name = directory_name;
  1025. info->indextable[i].file_name = file_name;
  1026. info->indextable[i].function_name = NULL;
  1027. ++i;
  1028. }
  1029. directory_name = NULL;
  1030. file_name = (char *) str + bfd_get_32 (abfd, stab + STRDXOFF);
  1031. if (file_name == (char *) str)
  1032. {
  1033. file_name = NULL;
  1034. nul_fun = NULL;
  1035. }
  1036. else
  1037. {
  1038. nul_fun = stab;
  1039. nul_str = str;
  1040. if (file_name >= (char *) info->strs + strsize || file_name < (char *) str)
  1041. file_name = NULL;
  1042. if (stab + STABSIZE + TYPEOFF < info->stabs + stabsize
  1043. && *(stab + STABSIZE + TYPEOFF) == (bfd_byte) N_SO)
  1044. {
  1045. /* Two consecutive N_SOs are a directory and a
  1046. file name. */
  1047. stab += STABSIZE;
  1048. directory_name = file_name;
  1049. file_name = ((char *) str
  1050. + bfd_get_32 (abfd, stab + STRDXOFF));
  1051. if (file_name >= (char *) info->strs + strsize || file_name < (char *) str)
  1052. file_name = NULL;
  1053. }
  1054. }
  1055. break;
  1056. case N_SOL:
  1057. /* The name of an include file. */
  1058. file_name = (char *) str + bfd_get_32 (abfd, stab + STRDXOFF);
  1059. /* PR 17512: file: 0c680a1f. */
  1060. /* PR 17512: file: 5da8aec4. */
  1061. if (file_name >= (char *) info->strs + strsize || file_name < (char *) str)
  1062. file_name = NULL;
  1063. break;
  1064. case N_FUN:
  1065. /* A function name. */
  1066. function_name = (char *) str + bfd_get_32 (abfd, stab + STRDXOFF);
  1067. if (function_name == (char *) str)
  1068. continue;
  1069. if (function_name >= (char *) info->strs + strsize)
  1070. function_name = NULL;
  1071. nul_fun = NULL;
  1072. info->indextable[i].val = bfd_get_32 (abfd, stab + VALOFF);
  1073. info->indextable[i].stab = stab;
  1074. info->indextable[i].str = str;
  1075. info->indextable[i].directory_name = directory_name;
  1076. info->indextable[i].file_name = file_name;
  1077. info->indextable[i].function_name = function_name;
  1078. ++i;
  1079. break;
  1080. }
  1081. }
  1082. if (nul_fun != NULL)
  1083. {
  1084. info->indextable[i].val = bfd_get_32 (abfd, nul_fun + VALOFF);
  1085. info->indextable[i].stab = nul_fun;
  1086. info->indextable[i].str = nul_str;
  1087. info->indextable[i].directory_name = directory_name;
  1088. info->indextable[i].file_name = file_name;
  1089. info->indextable[i].function_name = NULL;
  1090. ++i;
  1091. }
  1092. info->indextable[i].val = (bfd_vma) -1;
  1093. info->indextable[i].stab = info->stabs + stabsize;
  1094. info->indextable[i].str = str;
  1095. info->indextable[i].directory_name = NULL;
  1096. info->indextable[i].file_name = NULL;
  1097. info->indextable[i].function_name = NULL;
  1098. ++i;
  1099. info->indextablesize = i;
  1100. qsort (info->indextable, (size_t) i, sizeof (struct indexentry),
  1101. cmpindexentry);
  1102. *pinfo = info;
  1103. }
  1104. /* We are passed a section relative offset. The offsets in the
  1105. stabs information are absolute. */
  1106. offset += bfd_get_section_vma (abfd, section);
  1107. #ifdef ENABLE_CACHING
  1108. if (info->cached_indexentry != NULL
  1109. && offset >= info->cached_offset
  1110. && offset < (info->cached_indexentry + 1)->val)
  1111. {
  1112. stab = info->cached_stab;
  1113. indexentry = info->cached_indexentry;
  1114. file_name = info->cached_file_name;
  1115. }
  1116. else
  1117. #endif
  1118. {
  1119. long low, high;
  1120. long mid = -1;
  1121. /* Cache non-existent or invalid. Do binary search on
  1122. indextable. */
  1123. indexentry = NULL;
  1124. low = 0;
  1125. high = info->indextablesize - 1;
  1126. while (low != high)
  1127. {
  1128. mid = (high + low) / 2;
  1129. if (offset >= info->indextable[mid].val
  1130. && offset < info->indextable[mid + 1].val)
  1131. {
  1132. indexentry = &info->indextable[mid];
  1133. break;
  1134. }
  1135. if (info->indextable[mid].val > offset)
  1136. high = mid;
  1137. else
  1138. low = mid + 1;
  1139. }
  1140. if (indexentry == NULL)
  1141. return TRUE;
  1142. stab = indexentry->stab + STABSIZE;
  1143. file_name = indexentry->file_name;
  1144. }
  1145. directory_name = indexentry->directory_name;
  1146. str = indexentry->str;
  1147. saw_line = FALSE;
  1148. saw_func = FALSE;
  1149. for (; stab < (indexentry+1)->stab; stab += STABSIZE)
  1150. {
  1151. bfd_boolean done;
  1152. bfd_vma val;
  1153. done = FALSE;
  1154. switch (stab[TYPEOFF])
  1155. {
  1156. case N_SOL:
  1157. /* The name of an include file. */
  1158. val = bfd_get_32 (abfd, stab + VALOFF);
  1159. if (val <= offset)
  1160. {
  1161. file_name = (char *) str + bfd_get_32 (abfd, stab + STRDXOFF);
  1162. if (file_name >= (char *) info->strs + strsize || file_name < (char *) str)
  1163. file_name = NULL;
  1164. *pline = 0;
  1165. }
  1166. break;
  1167. case N_SLINE:
  1168. case N_DSLINE:
  1169. case N_BSLINE:
  1170. /* A line number. If the function was specified, then the value
  1171. is relative to the start of the function. Otherwise, the
  1172. value is an absolute address. */
  1173. val = ((indexentry->function_name ? indexentry->val : 0)
  1174. + bfd_get_32 (abfd, stab + VALOFF));
  1175. /* If this line starts before our desired offset, or if it's
  1176. the first line we've been able to find, use it. The
  1177. !saw_line check works around a bug in GCC 2.95.3, which emits
  1178. the first N_SLINE late. */
  1179. if (!saw_line || val <= offset)
  1180. {
  1181. *pline = bfd_get_16 (abfd, stab + DESCOFF);
  1182. #ifdef ENABLE_CACHING
  1183. info->cached_stab = stab;
  1184. info->cached_offset = val;
  1185. info->cached_file_name = file_name;
  1186. info->cached_indexentry = indexentry;
  1187. #endif
  1188. }
  1189. if (val > offset)
  1190. done = TRUE;
  1191. saw_line = TRUE;
  1192. break;
  1193. case N_FUN:
  1194. case N_SO:
  1195. if (saw_func || saw_line)
  1196. done = TRUE;
  1197. saw_func = TRUE;
  1198. break;
  1199. }
  1200. if (done)
  1201. break;
  1202. }
  1203. *pfound = TRUE;
  1204. if (file_name == NULL || IS_ABSOLUTE_PATH (file_name)
  1205. || directory_name == NULL)
  1206. *pfilename = file_name;
  1207. else
  1208. {
  1209. size_t dirlen;
  1210. dirlen = strlen (directory_name);
  1211. if (info->filename == NULL
  1212. || filename_ncmp (info->filename, directory_name, dirlen) != 0
  1213. || filename_cmp (info->filename + dirlen, file_name) != 0)
  1214. {
  1215. size_t len;
  1216. /* Don't free info->filename here. objdump and other
  1217. apps keep a copy of a previously returned file name
  1218. pointer. */
  1219. len = strlen (file_name) + 1;
  1220. info->filename = (char *) bfd_alloc (abfd, dirlen + len);
  1221. if (info->filename == NULL)
  1222. return FALSE;
  1223. memcpy (info->filename, directory_name, dirlen);
  1224. memcpy (info->filename + dirlen, file_name, len);
  1225. }
  1226. *pfilename = info->filename;
  1227. }
  1228. if (indexentry->function_name != NULL)
  1229. {
  1230. char *s;
  1231. /* This will typically be something like main:F(0,1), so we want
  1232. to clobber the colon. It's OK to change the name, since the
  1233. string is in our own local storage anyhow. */
  1234. s = strchr (indexentry->function_name, ':');
  1235. if (s != NULL)
  1236. *s = '\0';
  1237. *pfnname = indexentry->function_name;
  1238. }
  1239. return TRUE;
  1240. }