xcoff.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  1. /* xcoff.c -- Get debug data from an XCOFF file for backtraces.
  2. Copyright (C) 2012-2021 Free Software Foundation, Inc.
  3. Adapted from elf.c.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. (1) Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. (2) Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. (3) The name of the author may not be used to
  14. endorse or promote products derived from this software without
  15. specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  20. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  25. IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. POSSIBILITY OF SUCH DAMAGE. */
  27. #include "config.h"
  28. #include <errno.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <sys/types.h>
  32. #ifdef HAVE_LOADQUERY
  33. #include <sys/ldr.h>
  34. #endif
  35. #include "backtrace.h"
  36. #include "internal.h"
  37. /* The configure script must tell us whether we are 32-bit or 64-bit
  38. XCOFF. We could make this code test and support either possibility,
  39. but there is no point. This code only works for the currently
  40. running executable, which means that we know the XCOFF mode at
  41. configure time. */
  42. #if BACKTRACE_XCOFF_SIZE != 32 && BACKTRACE_XCOFF_SIZE != 64
  43. #error "Unknown BACKTRACE_XCOFF_SIZE"
  44. #endif
  45. /* XCOFF file header. */
  46. #if BACKTRACE_XCOFF_SIZE == 32
  47. typedef struct {
  48. uint16_t f_magic;
  49. uint16_t f_nscns;
  50. uint32_t f_timdat;
  51. uint32_t f_symptr;
  52. uint32_t f_nsyms;
  53. uint16_t f_opthdr;
  54. uint16_t f_flags;
  55. } b_xcoff_filhdr;
  56. #define XCOFF_MAGIC 0737
  57. #else /* BACKTRACE_XCOFF_SIZE != 32 */
  58. typedef struct {
  59. uint16_t f_magic;
  60. uint16_t f_nscns;
  61. uint32_t f_timdat;
  62. uint64_t f_symptr;
  63. uint16_t f_opthdr;
  64. uint16_t f_flags;
  65. uint32_t f_nsyms;
  66. } b_xcoff_filhdr;
  67. #define XCOFF_MAGIC 0767
  68. #endif /* BACKTRACE_XCOFF_SIZE != 32 */
  69. #define F_SHROBJ 0x2000 /* File is a shared object. */
  70. /* XCOFF section header. */
  71. #if BACKTRACE_XCOFF_SIZE == 32
  72. typedef struct {
  73. char s_name[8];
  74. uint32_t s_paddr;
  75. uint32_t s_vaddr;
  76. uint32_t s_size;
  77. uint32_t s_scnptr;
  78. uint32_t s_relptr;
  79. uint32_t s_lnnoptr;
  80. uint16_t s_nreloc;
  81. uint16_t s_nlnno;
  82. uint32_t s_flags;
  83. } b_xcoff_scnhdr;
  84. #define _OVERFLOW_MARKER 65535
  85. #else /* BACKTRACE_XCOFF_SIZE != 32 */
  86. typedef struct {
  87. char name[8];
  88. uint64_t s_paddr;
  89. uint64_t s_vaddr;
  90. uint64_t s_size;
  91. uint64_t s_scnptr;
  92. uint64_t s_relptr;
  93. uint64_t s_lnnoptr;
  94. uint32_t s_nreloc;
  95. uint32_t s_nlnno;
  96. uint32_t s_flags;
  97. } b_xcoff_scnhdr;
  98. #endif /* BACKTRACE_XCOFF_SIZE != 32 */
  99. #define STYP_DWARF 0x10 /* DWARF debugging section. */
  100. #define STYP_TEXT 0x20 /* Executable text (code) section. */
  101. #define STYP_OVRFLO 0x8000 /* Line-number field overflow section. */
  102. #define SSUBTYP_DWINFO 0x10000 /* DWARF info section. */
  103. #define SSUBTYP_DWLINE 0x20000 /* DWARF line-number section. */
  104. #define SSUBTYP_DWARNGE 0x50000 /* DWARF aranges section. */
  105. #define SSUBTYP_DWABREV 0x60000 /* DWARF abbreviation section. */
  106. #define SSUBTYP_DWSTR 0x70000 /* DWARF strings section. */
  107. #define SSUBTYP_DWRNGES 0x80000 /* DWARF ranges section. */
  108. /* XCOFF symbol. */
  109. #define SYMNMLEN 8
  110. #if BACKTRACE_XCOFF_SIZE == 32
  111. typedef struct {
  112. union {
  113. char _name[SYMNMLEN];
  114. struct {
  115. uint32_t _zeroes;
  116. uint32_t _offset;
  117. } _s;
  118. } _u;
  119. #define n_name _u._name
  120. #define n_zeroes _u._s._zeroes
  121. #define n_offset_ _u._s._offset
  122. uint32_t n_value;
  123. int16_t n_scnum;
  124. uint16_t n_type;
  125. uint8_t n_sclass;
  126. uint8_t n_numaux;
  127. } __attribute__ ((packed)) b_xcoff_syment;
  128. #else /* BACKTRACE_XCOFF_SIZE != 32 */
  129. typedef struct {
  130. uint64_t n_value;
  131. uint32_t n_offset_;
  132. int16_t n_scnum;
  133. uint16_t n_type;
  134. uint8_t n_sclass;
  135. uint8_t n_numaux;
  136. } __attribute__ ((packed)) b_xcoff_syment;
  137. #endif /* BACKTRACE_XCOFF_SIZE != 32 */
  138. #define SYMESZ 18
  139. #define C_EXT 2 /* External symbol. */
  140. #define C_FCN 101 /* Beginning or end of function. */
  141. #define C_FILE 103 /* Source file name. */
  142. #define C_HIDEXT 107 /* Unnamed external symbol. */
  143. #define C_BINCL 108 /* Beginning of include file. */
  144. #define C_EINCL 109 /* End of include file. */
  145. #define C_WEAKEXT 111 /* Weak external symbol. */
  146. #define ISFCN(x) ((x) & 0x0020)
  147. /* XCOFF AUX entry. */
  148. #define AUXESZ 18
  149. #define FILNMLEN 14
  150. typedef union {
  151. #if BACKTRACE_XCOFF_SIZE == 32
  152. struct {
  153. uint16_t pad;
  154. uint16_t x_lnnohi;
  155. uint16_t x_lnno;
  156. } x_block;
  157. #else
  158. struct {
  159. uint32_t x_lnno;
  160. } x_block;
  161. #endif
  162. union {
  163. char x_fname[FILNMLEN];
  164. struct {
  165. uint32_t x_zeroes;
  166. uint32_t x_offset;
  167. char pad[FILNMLEN-8];
  168. uint8_t x_ftype;
  169. } _x;
  170. } x_file;
  171. #if BACKTRACE_XCOFF_SIZE == 32
  172. struct {
  173. uint32_t x_exptr;
  174. uint32_t x_fsize;
  175. uint32_t x_lnnoptr;
  176. uint32_t x_endndx;
  177. } x_fcn;
  178. #else
  179. struct {
  180. uint64_t x_lnnoptr;
  181. uint32_t x_fsize;
  182. uint32_t x_endndx;
  183. } x_fcn;
  184. #endif
  185. struct {
  186. uint8_t pad[AUXESZ-1];
  187. uint8_t x_auxtype;
  188. } x_auxtype;
  189. } __attribute__ ((packed)) b_xcoff_auxent;
  190. /* XCOFF line number entry. */
  191. #if BACKTRACE_XCOFF_SIZE == 32
  192. typedef struct {
  193. union {
  194. uint32_t l_symndx;
  195. uint32_t l_paddr;
  196. } l_addr;
  197. uint16_t l_lnno;
  198. } b_xcoff_lineno;
  199. #define LINESZ 6
  200. #else /* BACKTRACE_XCOFF_SIZE != 32 */
  201. typedef struct {
  202. union {
  203. uint32_t l_symndx;
  204. uint64_t l_paddr;
  205. } l_addr;
  206. uint32_t l_lnno;
  207. } b_xcoff_lineno;
  208. #define LINESZ 12
  209. #endif /* BACKTRACE_XCOFF_SIZE != 32 */
  210. #if BACKTRACE_XCOFF_SIZE == 32
  211. #define XCOFF_AIX_TEXTBASE 0x10000000u
  212. #else
  213. #define XCOFF_AIX_TEXTBASE 0x100000000ul
  214. #endif
  215. /* AIX big archive fixed-length header. */
  216. #define AIAMAGBIG "<bigaf>\n"
  217. typedef struct {
  218. char fl_magic[8]; /* Archive magic string. */
  219. char fl_memoff[20]; /* Offset to member table. */
  220. char fl_gstoff[20]; /* Offset to global symbol table. */
  221. char fl_gst64off[20]; /* Offset to global symbol table for 64-bit objects. */
  222. char fl_fstmoff[20]; /* Offset to first archive member. */
  223. char fl_freeoff[20]; /* Offset to first member on free list. */
  224. } b_ar_fl_hdr;
  225. /* AIX big archive file member header. */
  226. typedef struct {
  227. char ar_size[20]; /* File member size - decimal. */
  228. char ar_nxtmem[20]; /* Next member offset - decimal. */
  229. char ar_prvmem[20]; /* Previous member offset - decimal. */
  230. char ar_date[12]; /* File member date - decimal. */
  231. char ar_uid[12]; /* File member userid - decimal. */
  232. char ar_gid[12]; /* File member group id - decimal. */
  233. char ar_mode[12]; /* File member mode - octal. */
  234. char ar_namlen[4]; /* File member name length - decimal. */
  235. char ar_name[2]; /* Start of member name. */
  236. } b_ar_hdr;
  237. /* Information we keep for an XCOFF symbol. */
  238. struct xcoff_symbol
  239. {
  240. /* The name of the symbol. */
  241. const char *name;
  242. /* The address of the symbol. */
  243. uintptr_t address;
  244. /* The size of the symbol. */
  245. size_t size;
  246. };
  247. /* Information to pass to xcoff_syminfo. */
  248. struct xcoff_syminfo_data
  249. {
  250. /* Symbols for the next module. */
  251. struct xcoff_syminfo_data *next;
  252. /* The XCOFF symbols, sorted by address. */
  253. struct xcoff_symbol *symbols;
  254. /* The number of symbols. */
  255. size_t count;
  256. };
  257. /* Information about an include file. */
  258. struct xcoff_incl
  259. {
  260. /* File name. */
  261. const char *filename;
  262. /* Offset to first line number from the include file. */
  263. uintptr_t begin;
  264. /* Offset to last line number from the include file. */
  265. uintptr_t end;
  266. };
  267. /* A growable vector of include files information. */
  268. struct xcoff_incl_vector
  269. {
  270. /* Memory. This is an array of struct xcoff_incl. */
  271. struct backtrace_vector vec;
  272. /* Number of include files. */
  273. size_t count;
  274. };
  275. /* A growable vector of functions information. */
  276. struct xcoff_func
  277. {
  278. /* PC. */
  279. uintptr_t pc;
  280. /* The size of the function. */
  281. size_t size;
  282. /* Function name. */
  283. const char *name;
  284. /* File name. */
  285. const char *filename;
  286. /* Pointer to first lnno entry. */
  287. uintptr_t lnnoptr;
  288. /* Base address of containing section. */
  289. uintptr_t sect_base;
  290. /* Starting source line number. */
  291. int lnno;
  292. };
  293. /* A growable vector of function information. This is used while
  294. reading the function symbols. */
  295. struct xcoff_func_vector
  296. {
  297. /* Memory. This is an array of struct xcoff_func. */
  298. struct backtrace_vector vec;
  299. /* Number of valid mappings. */
  300. size_t count;
  301. };
  302. /* The information we need to map a PC to a file and line. */
  303. struct xcoff_fileline_data
  304. {
  305. /* The data for the next file we know about. */
  306. struct xcoff_fileline_data *next;
  307. /* Functions information. */
  308. struct xcoff_func_vector func_vec;
  309. /* Include files information. */
  310. struct xcoff_incl_vector incl_vec;
  311. /* Line numbers information. */
  312. const unsigned char *linenos;
  313. size_t linenos_size;
  314. uint64_t lnnoptr0;
  315. /* Loader address. */
  316. uintptr_t base_address;
  317. };
  318. /* Information we gather for the DWARF sections we care about. */
  319. struct dwsect_info
  320. {
  321. /* Section file offset. */
  322. off_t offset;
  323. /* Section size. */
  324. size_t size;
  325. /* Section contents, after read from file. */
  326. const unsigned char *data;
  327. };
  328. /* A dummy callback function used when we can't find any debug info. */
  329. static int
  330. xcoff_nodebug (struct backtrace_state *state ATTRIBUTE_UNUSED,
  331. uintptr_t pc ATTRIBUTE_UNUSED,
  332. backtrace_full_callback callback ATTRIBUTE_UNUSED,
  333. backtrace_error_callback error_callback, void *data)
  334. {
  335. error_callback (data, "no debug info in XCOFF executable", -1);
  336. return 0;
  337. }
  338. /* A dummy callback function used when we can't find a symbol
  339. table. */
  340. static void
  341. xcoff_nosyms (struct backtrace_state *state ATTRIBUTE_UNUSED,
  342. uintptr_t addr ATTRIBUTE_UNUSED,
  343. backtrace_syminfo_callback callback ATTRIBUTE_UNUSED,
  344. backtrace_error_callback error_callback, void *data)
  345. {
  346. error_callback (data, "no symbol table in XCOFF executable", -1);
  347. }
  348. /* Compare struct xcoff_symbol for qsort. */
  349. static int
  350. xcoff_symbol_compare (const void *v1, const void *v2)
  351. {
  352. const struct xcoff_symbol *e1 = (const struct xcoff_symbol *) v1;
  353. const struct xcoff_symbol *e2 = (const struct xcoff_symbol *) v2;
  354. if (e1->address < e2->address)
  355. return -1;
  356. else if (e1->address > e2->address)
  357. return 1;
  358. else
  359. return 0;
  360. }
  361. /* Compare an ADDR against an xcoff_symbol for bsearch. */
  362. static int
  363. xcoff_symbol_search (const void *vkey, const void *ventry)
  364. {
  365. const uintptr_t *key = (const uintptr_t *) vkey;
  366. const struct xcoff_symbol *entry = (const struct xcoff_symbol *) ventry;
  367. uintptr_t addr;
  368. addr = *key;
  369. if (addr < entry->address)
  370. return -1;
  371. else if ((entry->size == 0 && addr > entry->address)
  372. || (entry->size > 0 && addr >= entry->address + entry->size))
  373. return 1;
  374. else
  375. return 0;
  376. }
  377. /* Add XDATA to the list in STATE. */
  378. static void
  379. xcoff_add_syminfo_data (struct backtrace_state *state,
  380. struct xcoff_syminfo_data *xdata)
  381. {
  382. if (!state->threaded)
  383. {
  384. struct xcoff_syminfo_data **pp;
  385. for (pp = (struct xcoff_syminfo_data **) (void *) &state->syminfo_data;
  386. *pp != NULL;
  387. pp = &(*pp)->next)
  388. ;
  389. *pp = xdata;
  390. }
  391. else
  392. {
  393. while (1)
  394. {
  395. struct xcoff_syminfo_data **pp;
  396. pp = (struct xcoff_syminfo_data **) (void *) &state->syminfo_data;
  397. while (1)
  398. {
  399. struct xcoff_syminfo_data *p;
  400. p = backtrace_atomic_load_pointer (pp);
  401. if (p == NULL)
  402. break;
  403. pp = &p->next;
  404. }
  405. if (__sync_bool_compare_and_swap (pp, NULL, xdata))
  406. break;
  407. }
  408. }
  409. }
  410. /* Return the symbol name and value for an ADDR. */
  411. static void
  412. xcoff_syminfo (struct backtrace_state *state ATTRIBUTE_UNUSED, uintptr_t addr,
  413. backtrace_syminfo_callback callback,
  414. backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
  415. void *data)
  416. {
  417. struct xcoff_syminfo_data *edata;
  418. struct xcoff_symbol *sym = NULL;
  419. const char *name;
  420. if (!state->threaded)
  421. {
  422. for (edata = (struct xcoff_syminfo_data *) state->syminfo_data;
  423. edata != NULL;
  424. edata = edata->next)
  425. {
  426. sym = ((struct xcoff_symbol *)
  427. bsearch (&addr, edata->symbols, edata->count,
  428. sizeof (struct xcoff_symbol), xcoff_symbol_search));
  429. if (sym != NULL)
  430. break;
  431. }
  432. }
  433. else
  434. {
  435. struct xcoff_syminfo_data **pp;
  436. pp = (struct xcoff_syminfo_data **) (void *) &state->syminfo_data;
  437. while (1)
  438. {
  439. edata = backtrace_atomic_load_pointer (pp);
  440. if (edata == NULL)
  441. break;
  442. sym = ((struct xcoff_symbol *)
  443. bsearch (&addr, edata->symbols, edata->count,
  444. sizeof (struct xcoff_symbol), xcoff_symbol_search));
  445. if (sym != NULL)
  446. break;
  447. pp = &edata->next;
  448. }
  449. }
  450. if (sym == NULL)
  451. callback (data, addr, NULL, 0, 0);
  452. else
  453. {
  454. name = sym->name;
  455. /* AIX prepends a '.' to function entry points, remove it. */
  456. if (name && *name == '.')
  457. ++name;
  458. callback (data, addr, name, sym->address, sym->size);
  459. }
  460. }
  461. /* Return the name of an XCOFF symbol. */
  462. static const char *
  463. xcoff_symname (const b_xcoff_syment *asym,
  464. const unsigned char *strtab, size_t strtab_size)
  465. {
  466. #if BACKTRACE_XCOFF_SIZE == 32
  467. if (asym->n_zeroes != 0)
  468. {
  469. /* Make a copy as we will release the symtab view. */
  470. char name[SYMNMLEN+1];
  471. strncpy (name, asym->n_name, SYMNMLEN);
  472. name[SYMNMLEN] = '\0';
  473. return strdup (name);
  474. }
  475. #endif
  476. if (asym->n_sclass & 0x80)
  477. return NULL; /* .debug */
  478. if (asym->n_offset_ >= strtab_size)
  479. return NULL;
  480. return (const char *) strtab + asym->n_offset_;
  481. }
  482. /* Initialize the symbol table info for xcoff_syminfo. */
  483. static int
  484. xcoff_initialize_syminfo (struct backtrace_state *state,
  485. uintptr_t base_address,
  486. const b_xcoff_syment *syms, size_t nsyms,
  487. const unsigned char *strtab, size_t strtab_size,
  488. backtrace_error_callback error_callback, void *data,
  489. struct xcoff_syminfo_data *sdata)
  490. {
  491. size_t xcoff_symbol_count;
  492. size_t xcoff_symbol_size;
  493. struct xcoff_symbol *xcoff_symbols;
  494. size_t i;
  495. unsigned int j;
  496. /* We only care about function symbols. Count them. */
  497. xcoff_symbol_count = 0;
  498. for (i = 0; i < nsyms; ++i)
  499. {
  500. const b_xcoff_syment *asym = &syms[i];
  501. if ((asym->n_sclass == C_EXT || asym->n_sclass == C_HIDEXT
  502. || asym->n_sclass == C_WEAKEXT)
  503. && ISFCN (asym->n_type) && asym->n_numaux > 0 && asym->n_scnum > 0)
  504. ++xcoff_symbol_count;
  505. i += asym->n_numaux;
  506. }
  507. xcoff_symbol_size = xcoff_symbol_count * sizeof (struct xcoff_symbol);
  508. xcoff_symbols = ((struct xcoff_symbol *)
  509. backtrace_alloc (state, xcoff_symbol_size, error_callback,
  510. data));
  511. if (xcoff_symbols == NULL)
  512. return 0;
  513. j = 0;
  514. for (i = 0; i < nsyms; ++i)
  515. {
  516. const b_xcoff_syment *asym = &syms[i];
  517. if ((asym->n_sclass == C_EXT || asym->n_sclass == C_HIDEXT
  518. || asym->n_sclass == C_WEAKEXT)
  519. && ISFCN (asym->n_type) && asym->n_numaux > 0 && asym->n_scnum > 0)
  520. {
  521. const b_xcoff_auxent *aux = (const b_xcoff_auxent *) (asym + 1);
  522. xcoff_symbols[j].name = xcoff_symname (asym, strtab, strtab_size);
  523. xcoff_symbols[j].address = base_address + asym->n_value;
  524. /* x_fsize will be 0 if there is no debug information. */
  525. xcoff_symbols[j].size = aux->x_fcn.x_fsize;
  526. ++j;
  527. }
  528. i += asym->n_numaux;
  529. }
  530. backtrace_qsort (xcoff_symbols, xcoff_symbol_count,
  531. sizeof (struct xcoff_symbol), xcoff_symbol_compare);
  532. sdata->next = NULL;
  533. sdata->symbols = xcoff_symbols;
  534. sdata->count = xcoff_symbol_count;
  535. return 1;
  536. }
  537. /* Compare struct xcoff_func for qsort. */
  538. static int
  539. xcoff_func_compare (const void *v1, const void *v2)
  540. {
  541. const struct xcoff_func *fn1 = (const struct xcoff_func *) v1;
  542. const struct xcoff_func *fn2 = (const struct xcoff_func *) v2;
  543. if (fn1->pc < fn2->pc)
  544. return -1;
  545. else if (fn1->pc > fn2->pc)
  546. return 1;
  547. else
  548. return 0;
  549. }
  550. /* Compare a PC against an xcoff_func for bsearch. */
  551. static int
  552. xcoff_func_search (const void *vkey, const void *ventry)
  553. {
  554. const uintptr_t *key = (const uintptr_t *) vkey;
  555. const struct xcoff_func *entry = (const struct xcoff_func *) ventry;
  556. uintptr_t pc;
  557. pc = *key;
  558. if (pc < entry->pc)
  559. return -1;
  560. else if ((entry->size == 0 && pc > entry->pc)
  561. || (entry->size > 0 && pc >= entry->pc + entry->size))
  562. return 1;
  563. else
  564. return 0;
  565. }
  566. /* Compare struct xcoff_incl for qsort. */
  567. static int
  568. xcoff_incl_compare (const void *v1, const void *v2)
  569. {
  570. const struct xcoff_incl *in1 = (const struct xcoff_incl *) v1;
  571. const struct xcoff_incl *in2 = (const struct xcoff_incl *) v2;
  572. if (in1->begin < in2->begin)
  573. return -1;
  574. else if (in1->begin > in2->begin)
  575. return 1;
  576. else
  577. return 0;
  578. }
  579. /* Find a lnnoptr in an include file. */
  580. static int
  581. xcoff_incl_search (const void *vkey, const void *ventry)
  582. {
  583. const uintptr_t *key = (const uintptr_t *) vkey;
  584. const struct xcoff_incl *entry = (const struct xcoff_incl *) ventry;
  585. uintptr_t lnno;
  586. lnno = *key;
  587. if (lnno < entry->begin)
  588. return -1;
  589. else if (lnno > entry->end)
  590. return 1;
  591. else
  592. return 0;
  593. }
  594. /* Look for a PC in the function vector for one module. On success,
  595. call CALLBACK and return whatever it returns. On error, call
  596. ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
  597. 0 if not. */
  598. static int
  599. xcoff_lookup_pc (struct backtrace_state *state ATTRIBUTE_UNUSED,
  600. struct xcoff_fileline_data *fdata, uintptr_t pc,
  601. backtrace_full_callback callback,
  602. backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
  603. void *data, int *found)
  604. {
  605. const struct xcoff_incl *incl, *bincl;
  606. const struct xcoff_func *fn;
  607. const b_xcoff_lineno *lineno;
  608. const unsigned char *lineptr;
  609. const char *function;
  610. const char *filename;
  611. uintptr_t lnnoptr, match;
  612. uint32_t lnno = 0;
  613. *found = 1;
  614. if ((pc & 3) != 0)
  615. ++pc;
  616. /* Find the function first. */
  617. fn = ((struct xcoff_func *)
  618. bsearch (&pc, fdata->func_vec.vec.base, fdata->func_vec.count,
  619. sizeof (struct xcoff_func), xcoff_func_search));
  620. if (fn == NULL)
  621. {
  622. *found = 0;
  623. return 0;
  624. }
  625. filename = fn->filename;
  626. /* Find the line number next. */
  627. /* Skip first entry that points to symtab. */
  628. lnnoptr = fn->lnnoptr + LINESZ;
  629. match = lnnoptr;
  630. lineptr = fdata->linenos + (lnnoptr - fdata->lnnoptr0);
  631. while (lineptr + LINESZ <= fdata->linenos + fdata->linenos_size)
  632. {
  633. lineno = (const b_xcoff_lineno *) lineptr;
  634. if (lineno->l_lnno == 0)
  635. break;
  636. if (pc <= fdata->base_address + lineno->l_addr.l_paddr)
  637. break;
  638. match = lnnoptr;
  639. lnno = lineno->l_lnno;
  640. lnnoptr += LINESZ;
  641. lineptr += LINESZ;
  642. }
  643. /* If part of a function other than the beginning comes from an
  644. include file, the line numbers are absolute, rather than
  645. relative to the beginning of the function. */
  646. incl = ((struct xcoff_incl *)
  647. bsearch (&match, fdata->incl_vec.vec.base,
  648. fdata->incl_vec.count, sizeof (struct xcoff_incl),
  649. xcoff_incl_search));
  650. if (incl != NULL)
  651. {
  652. bincl = ((struct xcoff_incl *)
  653. bsearch (&fn->lnnoptr, fdata->incl_vec.vec.base,
  654. fdata->incl_vec.count, sizeof (struct xcoff_incl),
  655. xcoff_incl_search));
  656. if (bincl != NULL && strcmp (incl->filename, bincl->filename) == 0)
  657. {
  658. lnno += fn->lnno - 1;
  659. }
  660. filename = incl->filename;
  661. }
  662. else
  663. {
  664. lnno += fn->lnno - 1;
  665. }
  666. function = fn->name;
  667. /* AIX prepends a '.' to function entry points, remove it. */
  668. if (function != NULL && *function == '.')
  669. ++function;
  670. return callback (data, pc, filename, lnno, function);
  671. }
  672. /* Return the file/line information for a PC using the XCOFF lineno
  673. mapping we built earlier. */
  674. static int
  675. xcoff_fileline (struct backtrace_state *state, uintptr_t pc,
  676. backtrace_full_callback callback,
  677. backtrace_error_callback error_callback, void *data)
  678. {
  679. struct xcoff_fileline_data *fdata;
  680. int found;
  681. int ret;
  682. if (!state->threaded)
  683. {
  684. for (fdata = (struct xcoff_fileline_data *) state->fileline_data;
  685. fdata != NULL;
  686. fdata = fdata->next)
  687. {
  688. ret = xcoff_lookup_pc (state, fdata, pc, callback, error_callback,
  689. data, &found);
  690. if (ret != 0 || found)
  691. return ret;
  692. }
  693. }
  694. else
  695. {
  696. struct xcoff_fileline_data **pp;
  697. pp = (struct xcoff_fileline_data **) (void *) &state->fileline_data;
  698. while (1)
  699. {
  700. fdata = backtrace_atomic_load_pointer (pp);
  701. if (fdata == NULL)
  702. break;
  703. ret = xcoff_lookup_pc (state, fdata, pc, callback, error_callback,
  704. data, &found);
  705. if (ret != 0 || found)
  706. return ret;
  707. pp = &fdata->next;
  708. }
  709. }
  710. /* FIXME: See if any libraries have been dlopen'ed. */
  711. return callback (data, pc, NULL, 0, NULL);
  712. }
  713. /* Initialize the function vector info for xcoff_fileline. */
  714. static int
  715. xcoff_initialize_fileline (struct backtrace_state *state,
  716. uintptr_t base_address,
  717. const b_xcoff_scnhdr *sects,
  718. const b_xcoff_syment *syms, size_t nsyms,
  719. const unsigned char *strtab, size_t strtab_size,
  720. const unsigned char *linenos, size_t linenos_size,
  721. uint64_t lnnoptr0,
  722. backtrace_error_callback error_callback, void *data)
  723. {
  724. struct xcoff_fileline_data *fdata;
  725. struct xcoff_func *fn;
  726. const b_xcoff_syment *fsym;
  727. const b_xcoff_auxent *aux;
  728. const char *filename;
  729. const char *name;
  730. struct xcoff_incl *incl;
  731. uintptr_t begin, end;
  732. uintptr_t lnno, lnnoptr;
  733. uint32_t fsize;
  734. size_t i;
  735. fdata = ((struct xcoff_fileline_data *)
  736. backtrace_alloc (state, sizeof (struct xcoff_fileline_data),
  737. error_callback, data));
  738. if (fdata == NULL)
  739. return 0;
  740. memset (fdata, 0, sizeof *fdata);
  741. fdata->base_address = base_address;
  742. fdata->linenos = linenos;
  743. fdata->linenos_size = linenos_size;
  744. fdata->lnnoptr0 = lnnoptr0;
  745. begin = 0;
  746. filename = NULL;
  747. fsym = NULL;
  748. lnnoptr = 0;
  749. fsize = 0;
  750. for (i = 0; i < nsyms; ++i)
  751. {
  752. const b_xcoff_syment *asym = &syms[i];
  753. switch (asym->n_sclass)
  754. {
  755. case C_BINCL:
  756. begin = asym->n_value;
  757. break;
  758. case C_EINCL:
  759. if (begin == 0)
  760. break;
  761. end = asym->n_value;
  762. incl = ((struct xcoff_incl *)
  763. backtrace_vector_grow (state, sizeof (struct xcoff_incl),
  764. error_callback, data,
  765. &fdata->incl_vec.vec));
  766. if (incl != NULL)
  767. {
  768. incl->filename = xcoff_symname (asym, strtab, strtab_size);
  769. incl->begin = begin;
  770. incl->end = end;
  771. ++fdata->incl_vec.count;
  772. }
  773. begin = 0;
  774. break;
  775. case C_FILE:
  776. filename = xcoff_symname (asym, strtab, strtab_size);
  777. if (filename == NULL)
  778. break;
  779. /* If the file auxiliary entry is not used, the symbol name is
  780. the name of the source file. If the file auxiliary entry is
  781. used, then the symbol name should be .file, and the first
  782. file auxiliary entry (by convention) contains the source
  783. file name. */
  784. if (asym->n_numaux > 0 && strcmp (filename, ".file") == 0)
  785. {
  786. aux = (const b_xcoff_auxent *) (asym + 1);
  787. if (aux->x_file._x.x_zeroes != 0)
  788. {
  789. /* Make a copy as we will release the symtab view. */
  790. char name[FILNMLEN+1];
  791. strncpy (name, aux->x_file.x_fname, FILNMLEN);
  792. name[FILNMLEN] = '\0';
  793. filename = strdup (name);
  794. }
  795. else if (aux->x_file._x.x_offset < strtab_size)
  796. filename = (const char *) strtab + aux->x_file._x.x_offset;
  797. else
  798. filename = NULL;
  799. }
  800. break;
  801. case C_EXT:
  802. case C_HIDEXT:
  803. case C_WEAKEXT:
  804. fsym = NULL;
  805. lnnoptr = 0;
  806. fsize = 0;
  807. if (!ISFCN (asym->n_type) || asym->n_numaux == 0
  808. || asym->n_scnum <= 0)
  809. break;
  810. if (filename == NULL)
  811. break;
  812. aux = (const b_xcoff_auxent *) (asym + 1);
  813. lnnoptr = aux->x_fcn.x_lnnoptr;
  814. if (lnnoptr < lnnoptr0
  815. || lnnoptr + LINESZ > lnnoptr0 + linenos_size)
  816. break;
  817. /* x_fsize will be 0 if there is no debug information. */
  818. fsize = aux->x_fcn.x_fsize;
  819. fsym = asym;
  820. break;
  821. case C_FCN:
  822. if (asym->n_numaux == 0)
  823. break;
  824. if (fsym == NULL)
  825. break;
  826. name = xcoff_symname (asym, strtab, strtab_size);
  827. if (name == NULL || strcmp (name, ".bf") != 0)
  828. {
  829. fsym = NULL;
  830. break;
  831. }
  832. aux = (const b_xcoff_auxent *) (asym + 1);
  833. #if BACKTRACE_XCOFF_SIZE == 32
  834. lnno = (uint32_t) aux->x_block.x_lnnohi << 16
  835. | aux->x_block.x_lnno;
  836. #else
  837. lnno = aux->x_block.x_lnno;
  838. #endif
  839. fn = ((struct xcoff_func *)
  840. backtrace_vector_grow (state, sizeof (struct xcoff_func),
  841. error_callback, data,
  842. &fdata->func_vec.vec));
  843. if (fn == NULL)
  844. break;
  845. fn->name = xcoff_symname (fsym, strtab, strtab_size);
  846. fn->filename = filename;
  847. fn->sect_base = sects[fsym->n_scnum - 1].s_paddr;
  848. fn->pc = base_address + fsym->n_value;
  849. fn->size = fsize;
  850. fn->lnno = lnno;
  851. fn->lnnoptr = lnnoptr;
  852. ++fdata->func_vec.count;
  853. break;
  854. }
  855. i += asym->n_numaux;
  856. }
  857. if (!backtrace_vector_release (state, &fdata->func_vec.vec, error_callback,
  858. data))
  859. goto fail;
  860. backtrace_qsort (fdata->func_vec.vec.base, fdata->func_vec.count,
  861. sizeof (struct xcoff_func), xcoff_func_compare);
  862. if (!backtrace_vector_release (state, &fdata->incl_vec.vec, error_callback,
  863. data))
  864. goto fail;
  865. backtrace_qsort (fdata->incl_vec.vec.base, fdata->incl_vec.count,
  866. sizeof (struct xcoff_incl), xcoff_incl_compare);
  867. if (!state->threaded)
  868. {
  869. struct xcoff_fileline_data **pp;
  870. for (pp = (struct xcoff_fileline_data **) (void *) &state->fileline_data;
  871. *pp != NULL;
  872. pp = &(*pp)->next)
  873. ;
  874. *pp = fdata;
  875. }
  876. else
  877. {
  878. while (1)
  879. {
  880. struct xcoff_fileline_data **pp;
  881. pp = (struct xcoff_fileline_data **) (void *) &state->fileline_data;
  882. while (1)
  883. {
  884. struct xcoff_fileline_data *p;
  885. p = backtrace_atomic_load_pointer (pp);
  886. if (p == NULL)
  887. break;
  888. pp = &p->next;
  889. }
  890. if (__sync_bool_compare_and_swap (pp, NULL, fdata))
  891. break;
  892. }
  893. }
  894. return 1;
  895. fail:
  896. return 0;
  897. }
  898. /* Add the backtrace data for one XCOFF file. Returns 1 on success,
  899. 0 on failure (in both cases descriptor is closed). */
  900. static int
  901. xcoff_add (struct backtrace_state *state, int descriptor, off_t offset,
  902. uintptr_t base_address, backtrace_error_callback error_callback,
  903. void *data, fileline *fileline_fn, int *found_sym, int exe)
  904. {
  905. struct backtrace_view fhdr_view;
  906. struct backtrace_view sects_view;
  907. struct backtrace_view linenos_view;
  908. struct backtrace_view syms_view;
  909. struct backtrace_view str_view;
  910. struct backtrace_view dwarf_view;
  911. b_xcoff_filhdr fhdr;
  912. const b_xcoff_scnhdr *sects;
  913. const b_xcoff_scnhdr *stext;
  914. uint64_t lnnoptr;
  915. uint32_t nlnno;
  916. off_t str_off;
  917. off_t min_offset;
  918. off_t max_offset;
  919. struct dwsect_info dwsect[DEBUG_MAX];
  920. size_t sects_size;
  921. size_t syms_size;
  922. int32_t str_size;
  923. int sects_view_valid;
  924. int linenos_view_valid;
  925. int syms_view_valid;
  926. int str_view_valid;
  927. int dwarf_view_valid;
  928. int magic_ok;
  929. int i;
  930. struct dwarf_sections dwarf_sections;
  931. *found_sym = 0;
  932. sects_view_valid = 0;
  933. linenos_view_valid = 0;
  934. syms_view_valid = 0;
  935. str_view_valid = 0;
  936. dwarf_view_valid = 0;
  937. str_size = 0;
  938. /* Map the XCOFF file header. */
  939. if (!backtrace_get_view (state, descriptor, offset, sizeof (b_xcoff_filhdr),
  940. error_callback, data, &fhdr_view))
  941. goto fail;
  942. memcpy (&fhdr, fhdr_view.data, sizeof fhdr);
  943. magic_ok = (fhdr.f_magic == XCOFF_MAGIC);
  944. backtrace_release_view (state, &fhdr_view, error_callback, data);
  945. if (!magic_ok)
  946. {
  947. if (exe)
  948. error_callback (data, "executable file is not XCOFF", 0);
  949. goto fail;
  950. }
  951. /* Verify object is of expected type. */
  952. if ((exe && (fhdr.f_flags & F_SHROBJ))
  953. || (!exe && !(fhdr.f_flags & F_SHROBJ)))
  954. goto fail;
  955. /* Read the section headers. */
  956. sects_size = fhdr.f_nscns * sizeof (b_xcoff_scnhdr);
  957. if (!backtrace_get_view (state, descriptor,
  958. offset + sizeof (fhdr) + fhdr.f_opthdr,
  959. sects_size, error_callback, data, &sects_view))
  960. goto fail;
  961. sects_view_valid = 1;
  962. sects = (const b_xcoff_scnhdr *) sects_view.data;
  963. /* FIXME: assumes only one .text section. */
  964. for (i = 0; i < fhdr.f_nscns; ++i)
  965. if ((sects[i].s_flags & 0xffff) == STYP_TEXT)
  966. break;
  967. if (i == fhdr.f_nscns)
  968. goto fail;
  969. stext = &sects[i];
  970. /* base_address represents the difference between the
  971. virtual memory address of the shared object or a loaded
  972. executable and the offset of that object in the file
  973. from which it was loaded.
  974. On AIX, virtual address is either fixed for executable
  975. or given by ldinfo. This address will include the XCOFF
  976. headers. */
  977. base_address = ((exe ? XCOFF_AIX_TEXTBASE : base_address)
  978. + stext->s_scnptr
  979. - stext->s_paddr);
  980. lnnoptr = stext->s_lnnoptr;
  981. nlnno = stext->s_nlnno;
  982. #if BACKTRACE_XCOFF_SIZE == 32
  983. if (nlnno == _OVERFLOW_MARKER)
  984. {
  985. int sntext = i + 1;
  986. /* Find the matching .ovrflo section. */
  987. for (i = 0; i < fhdr.f_nscns; ++i)
  988. {
  989. if (((sects[i].s_flags & 0xffff) == STYP_OVRFLO)
  990. && sects[i].s_nlnno == sntext)
  991. {
  992. nlnno = sects[i].s_vaddr;
  993. break;
  994. }
  995. }
  996. }
  997. #endif
  998. /* Read the symbol table and the string table. */
  999. if (fhdr.f_symptr != 0)
  1000. {
  1001. struct xcoff_syminfo_data *sdata;
  1002. /* Symbol table is followed by the string table. The string table
  1003. starts with its length (on 4 bytes).
  1004. Map the symbol table and the length of the string table. */
  1005. syms_size = fhdr.f_nsyms * sizeof (b_xcoff_syment);
  1006. if (!backtrace_get_view (state, descriptor, offset + fhdr.f_symptr,
  1007. syms_size + 4, error_callback, data,
  1008. &syms_view))
  1009. goto fail;
  1010. syms_view_valid = 1;
  1011. memcpy (&str_size, syms_view.data + syms_size, 4);
  1012. str_off = fhdr.f_symptr + syms_size;
  1013. if (str_size > 4)
  1014. {
  1015. /* Map string table (including the length word). */
  1016. if (!backtrace_get_view (state, descriptor, offset + str_off,
  1017. str_size, error_callback, data, &str_view))
  1018. goto fail;
  1019. str_view_valid = 1;
  1020. }
  1021. sdata = ((struct xcoff_syminfo_data *)
  1022. backtrace_alloc (state, sizeof *sdata, error_callback, data));
  1023. if (sdata == NULL)
  1024. goto fail;
  1025. if (!xcoff_initialize_syminfo (state, base_address,
  1026. syms_view.data, fhdr.f_nsyms,
  1027. str_view.data, str_size,
  1028. error_callback, data, sdata))
  1029. {
  1030. backtrace_free (state, sdata, sizeof *sdata, error_callback, data);
  1031. goto fail;
  1032. }
  1033. *found_sym = 1;
  1034. xcoff_add_syminfo_data (state, sdata);
  1035. }
  1036. /* Read all the DWARF sections in a single view, since they are
  1037. probably adjacent in the file. We never release this view. */
  1038. min_offset = 0;
  1039. max_offset = 0;
  1040. memset (dwsect, 0, sizeof dwsect);
  1041. for (i = 0; i < fhdr.f_nscns; ++i)
  1042. {
  1043. off_t end;
  1044. int idx;
  1045. if ((sects[i].s_flags & 0xffff) != STYP_DWARF
  1046. || sects[i].s_size == 0)
  1047. continue;
  1048. /* Map DWARF section to array index. */
  1049. switch (sects[i].s_flags & 0xffff0000)
  1050. {
  1051. case SSUBTYP_DWINFO:
  1052. idx = DEBUG_INFO;
  1053. break;
  1054. case SSUBTYP_DWLINE:
  1055. idx = DEBUG_LINE;
  1056. break;
  1057. case SSUBTYP_DWABREV:
  1058. idx = DEBUG_ABBREV;
  1059. break;
  1060. case SSUBTYP_DWRNGES:
  1061. idx = DEBUG_RANGES;
  1062. break;
  1063. case SSUBTYP_DWSTR:
  1064. idx = DEBUG_STR;
  1065. break;
  1066. default:
  1067. continue;
  1068. }
  1069. if (min_offset == 0 || (off_t) sects[i].s_scnptr < min_offset)
  1070. min_offset = sects[i].s_scnptr;
  1071. end = sects[i].s_scnptr + sects[i].s_size;
  1072. if (end > max_offset)
  1073. max_offset = end;
  1074. dwsect[idx].offset = sects[i].s_scnptr;
  1075. dwsect[idx].size = sects[i].s_size;
  1076. }
  1077. if (min_offset != 0 && max_offset != 0)
  1078. {
  1079. if (!backtrace_get_view (state, descriptor, offset + min_offset,
  1080. max_offset - min_offset,
  1081. error_callback, data, &dwarf_view))
  1082. goto fail;
  1083. dwarf_view_valid = 1;
  1084. for (i = 0; i < (int) DEBUG_MAX; ++i)
  1085. {
  1086. if (dwsect[i].offset == 0)
  1087. dwsect[i].data = NULL;
  1088. else
  1089. dwsect[i].data = ((const unsigned char *) dwarf_view.data
  1090. + (dwsect[i].offset - min_offset));
  1091. }
  1092. memset (&dwarf_sections, 0, sizeof dwarf_sections);
  1093. dwarf_sections.data[DEBUG_INFO] = dwsect[DEBUG_INFO].data;
  1094. dwarf_sections.size[DEBUG_INFO] = dwsect[DEBUG_INFO].size;
  1095. dwarf_sections.data[DEBUG_LINE] = dwsect[DEBUG_LINE].data;
  1096. dwarf_sections.size[DEBUG_LINE] = dwsect[DEBUG_LINE].size;
  1097. dwarf_sections.data[DEBUG_ABBREV] = dwsect[DEBUG_ABBREV].data;
  1098. dwarf_sections.size[DEBUG_ABBREV] = dwsect[DEBUG_ABBREV].size;
  1099. dwarf_sections.data[DEBUG_RANGES] = dwsect[DEBUG_RANGES].data;
  1100. dwarf_sections.size[DEBUG_RANGES] = dwsect[DEBUG_RANGES].size;
  1101. dwarf_sections.data[DEBUG_STR] = dwsect[DEBUG_STR].data;
  1102. dwarf_sections.size[DEBUG_STR] = dwsect[DEBUG_STR].size;
  1103. if (!backtrace_dwarf_add (state, base_address, &dwarf_sections,
  1104. 1, /* big endian */
  1105. NULL, /* altlink */
  1106. error_callback, data, fileline_fn,
  1107. NULL /* returned fileline_entry */))
  1108. goto fail;
  1109. }
  1110. /* Read the XCOFF line number entries if DWARF sections not found. */
  1111. if (!dwarf_view_valid && fhdr.f_symptr != 0 && lnnoptr != 0)
  1112. {
  1113. size_t linenos_size = (size_t) nlnno * LINESZ;
  1114. /* We never release this view. */
  1115. if (!backtrace_get_view (state, descriptor, offset + lnnoptr,
  1116. linenos_size,
  1117. error_callback, data, &linenos_view))
  1118. goto fail;
  1119. linenos_view_valid = 1;
  1120. if (xcoff_initialize_fileline (state, base_address, sects,
  1121. syms_view.data, fhdr.f_nsyms,
  1122. str_view.data, str_size,
  1123. linenos_view.data, linenos_size,
  1124. lnnoptr, error_callback, data))
  1125. *fileline_fn = xcoff_fileline;
  1126. }
  1127. backtrace_release_view (state, &sects_view, error_callback, data);
  1128. sects_view_valid = 0;
  1129. if (syms_view_valid)
  1130. backtrace_release_view (state, &syms_view, error_callback, data);
  1131. syms_view_valid = 0;
  1132. /* We've read all we need from the executable. */
  1133. if (!backtrace_close (descriptor, error_callback, data))
  1134. goto fail;
  1135. descriptor = -1;
  1136. return 1;
  1137. fail:
  1138. if (sects_view_valid)
  1139. backtrace_release_view (state, &sects_view, error_callback, data);
  1140. if (str_view_valid)
  1141. backtrace_release_view (state, &str_view, error_callback, data);
  1142. if (syms_view_valid)
  1143. backtrace_release_view (state, &syms_view, error_callback, data);
  1144. if (linenos_view_valid)
  1145. backtrace_release_view (state, &linenos_view, error_callback, data);
  1146. if (dwarf_view_valid)
  1147. backtrace_release_view (state, &dwarf_view, error_callback, data);
  1148. if (descriptor != -1 && offset == 0)
  1149. backtrace_close (descriptor, error_callback, data);
  1150. return 0;
  1151. }
  1152. #ifdef HAVE_LOADQUERY
  1153. /* Read an integer value in human-readable format from an AIX
  1154. big archive fixed-length or member header. */
  1155. static int
  1156. xcoff_parse_decimal (const char *buf, size_t size, off_t *off)
  1157. {
  1158. char str[32];
  1159. char *end;
  1160. if (size >= sizeof str)
  1161. return 0;
  1162. memcpy (str, buf, size);
  1163. str[size] = '\0';
  1164. *off = strtol (str, &end, 10);
  1165. if (*end != '\0' && *end != ' ')
  1166. return 0;
  1167. return 1;
  1168. }
  1169. /* Add the backtrace data for a member of an AIX big archive.
  1170. Returns 1 on success, 0 on failure. */
  1171. static int
  1172. xcoff_armem_add (struct backtrace_state *state, int descriptor,
  1173. uintptr_t base_address, const char *member,
  1174. backtrace_error_callback error_callback, void *data,
  1175. fileline *fileline_fn, int *found_sym)
  1176. {
  1177. struct backtrace_view view;
  1178. b_ar_fl_hdr fl_hdr;
  1179. const b_ar_hdr *ar_hdr;
  1180. off_t off;
  1181. off_t len;
  1182. int memlen;
  1183. *found_sym = 0;
  1184. /* Map archive fixed-length header. */
  1185. if (!backtrace_get_view (state, descriptor, 0, sizeof (b_ar_fl_hdr),
  1186. error_callback, data, &view))
  1187. goto fail;
  1188. memcpy (&fl_hdr, view.data, sizeof (b_ar_fl_hdr));
  1189. backtrace_release_view (state, &view, error_callback, data);
  1190. if (memcmp (fl_hdr.fl_magic, AIAMAGBIG, 8) != 0)
  1191. goto fail;
  1192. memlen = strlen (member);
  1193. /* Read offset of first archive member. */
  1194. if (!xcoff_parse_decimal (fl_hdr.fl_fstmoff, sizeof fl_hdr.fl_fstmoff, &off))
  1195. goto fail;
  1196. while (off != 0)
  1197. {
  1198. /* Map archive member header and member name. */
  1199. if (!backtrace_get_view (state, descriptor, off,
  1200. sizeof (b_ar_hdr) + memlen,
  1201. error_callback, data, &view))
  1202. break;
  1203. ar_hdr = (const b_ar_hdr *) view.data;
  1204. /* Read archive member name length. */
  1205. if (!xcoff_parse_decimal (ar_hdr->ar_namlen, sizeof ar_hdr->ar_namlen,
  1206. &len))
  1207. {
  1208. backtrace_release_view (state, &view, error_callback, data);
  1209. break;
  1210. }
  1211. if (len == memlen && !memcmp (ar_hdr->ar_name, member, memlen))
  1212. {
  1213. off = (off + sizeof (b_ar_hdr) + memlen + 1) & ~1;
  1214. /* The archive can contain several members with the same name
  1215. (e.g. 32-bit and 64-bit), so continue if not ok. */
  1216. if (xcoff_add (state, descriptor, off, base_address, error_callback,
  1217. data, fileline_fn, found_sym, 0))
  1218. {
  1219. backtrace_release_view (state, &view, error_callback, data);
  1220. return 1;
  1221. }
  1222. }
  1223. /* Read offset of next archive member. */
  1224. if (!xcoff_parse_decimal (ar_hdr->ar_nxtmem, sizeof ar_hdr->ar_nxtmem,
  1225. &off))
  1226. {
  1227. backtrace_release_view (state, &view, error_callback, data);
  1228. break;
  1229. }
  1230. backtrace_release_view (state, &view, error_callback, data);
  1231. }
  1232. fail:
  1233. /* No matching member found. */
  1234. backtrace_close (descriptor, error_callback, data);
  1235. return 0;
  1236. }
  1237. /* Add the backtrace data for dynamically loaded libraries. */
  1238. static void
  1239. xcoff_add_shared_libs (struct backtrace_state *state,
  1240. backtrace_error_callback error_callback,
  1241. void *data, fileline *fileline_fn, int *found_sym)
  1242. {
  1243. const struct ld_info *ldinfo;
  1244. void *buf;
  1245. unsigned int buflen;
  1246. const char *member;
  1247. int descriptor;
  1248. int does_not_exist;
  1249. int lib_found_sym;
  1250. int ret;
  1251. /* Retrieve the list of loaded libraries. */
  1252. buf = NULL;
  1253. buflen = 512;
  1254. do
  1255. {
  1256. buf = realloc (buf, buflen);
  1257. if (buf == NULL)
  1258. {
  1259. ret = -1;
  1260. break;
  1261. }
  1262. ret = loadquery (L_GETINFO, buf, buflen);
  1263. if (ret == 0)
  1264. break;
  1265. buflen *= 2;
  1266. }
  1267. while (ret == -1 && errno == ENOMEM);
  1268. if (ret != 0)
  1269. {
  1270. free (buf);
  1271. return;
  1272. }
  1273. ldinfo = (const struct ld_info *) buf;
  1274. while ((const char *) ldinfo < (const char *) buf + buflen)
  1275. {
  1276. if (*ldinfo->ldinfo_filename != '/')
  1277. goto next;
  1278. descriptor = backtrace_open (ldinfo->ldinfo_filename, error_callback,
  1279. data, &does_not_exist);
  1280. if (descriptor < 0)
  1281. goto next;
  1282. /* Check if it is an archive (member name not empty). */
  1283. member = ldinfo->ldinfo_filename + strlen (ldinfo->ldinfo_filename) + 1;
  1284. if (*member)
  1285. {
  1286. xcoff_armem_add (state, descriptor,
  1287. (uintptr_t) ldinfo->ldinfo_textorg, member,
  1288. error_callback, data, fileline_fn, &lib_found_sym);
  1289. }
  1290. else
  1291. {
  1292. xcoff_add (state, descriptor, 0, (uintptr_t) ldinfo->ldinfo_textorg,
  1293. error_callback, data, fileline_fn, &lib_found_sym, 0);
  1294. }
  1295. if (lib_found_sym)
  1296. *found_sym = 1;
  1297. next:
  1298. if (ldinfo->ldinfo_next == 0)
  1299. break;
  1300. ldinfo = (const struct ld_info *) ((const char *) ldinfo
  1301. + ldinfo->ldinfo_next);
  1302. }
  1303. free (buf);
  1304. }
  1305. #endif /* HAVE_LOADQUERY */
  1306. /* Initialize the backtrace data we need from an XCOFF executable.
  1307. Returns 1 on success, 0 on failure. */
  1308. int
  1309. backtrace_initialize (struct backtrace_state *state,
  1310. const char *filename ATTRIBUTE_UNUSED, int descriptor,
  1311. backtrace_error_callback error_callback,
  1312. void *data, fileline *fileline_fn)
  1313. {
  1314. int ret;
  1315. int found_sym;
  1316. fileline xcoff_fileline_fn = xcoff_nodebug;
  1317. ret = xcoff_add (state, descriptor, 0, 0, error_callback, data,
  1318. &xcoff_fileline_fn, &found_sym, 1);
  1319. if (!ret)
  1320. return 0;
  1321. #ifdef HAVE_LOADQUERY
  1322. xcoff_add_shared_libs (state, error_callback, data, &xcoff_fileline_fn,
  1323. &found_sym);
  1324. #endif
  1325. if (!state->threaded)
  1326. {
  1327. if (found_sym)
  1328. state->syminfo_fn = xcoff_syminfo;
  1329. else if (state->syminfo_fn == NULL)
  1330. state->syminfo_fn = xcoff_nosyms;
  1331. }
  1332. else
  1333. {
  1334. if (found_sym)
  1335. backtrace_atomic_store_pointer (&state->syminfo_fn, xcoff_syminfo);
  1336. else
  1337. (void) __sync_bool_compare_and_swap (&state->syminfo_fn, NULL,
  1338. xcoff_nosyms);
  1339. }
  1340. if (!state->threaded)
  1341. {
  1342. if (state->fileline_fn == NULL || state->fileline_fn == xcoff_nodebug)
  1343. *fileline_fn = xcoff_fileline_fn;
  1344. }
  1345. else
  1346. {
  1347. fileline current_fn;
  1348. current_fn = backtrace_atomic_load_pointer (&state->fileline_fn);
  1349. if (current_fn == NULL || current_fn == xcoff_nodebug)
  1350. *fileline_fn = xcoff_fileline_fn;
  1351. }
  1352. return 1;
  1353. }