wrstabs.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272
  1. /* wrstabs.c -- Output stabs debugging information
  2. Copyright (C) 1996-2015 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor <ian@cygnus.com>.
  4. This file is part of GNU Binutils.
  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, MA
  16. 02110-1301, USA. */
  17. /* This file contains code which writes out stabs debugging
  18. information. */
  19. #include "sysdep.h"
  20. #include <assert.h>
  21. #include "bfd.h"
  22. #include "libiberty.h"
  23. #include "filenames.h"
  24. #include "safe-ctype.h"
  25. #include "bucomm.h"
  26. #include "debug.h"
  27. #include "budbg.h"
  28. #include "aout/aout64.h"
  29. #include "aout/stab_gnu.h"
  30. /* The size of a stabs symbol. This presumes 32 bit values. */
  31. #define STAB_SYMBOL_SIZE (12)
  32. /* An entry in a string hash table. */
  33. struct string_hash_entry
  34. {
  35. struct bfd_hash_entry root;
  36. /* Next string in this table. */
  37. struct string_hash_entry *next;
  38. /* Index in string table. */
  39. long index;
  40. /* Size of type if this is a typedef. */
  41. unsigned int size;
  42. };
  43. /* A string hash table. */
  44. struct string_hash_table
  45. {
  46. struct bfd_hash_table table;
  47. };
  48. /* The type stack. Each element on the stack is a string. */
  49. struct stab_type_stack
  50. {
  51. /* The next element on the stack. */
  52. struct stab_type_stack *next;
  53. /* This element as a string. */
  54. char *string;
  55. /* The type index of this element. */
  56. long index;
  57. /* The size of the type. */
  58. unsigned int size;
  59. /* Whether type string defines a new type. */
  60. bfd_boolean definition;
  61. /* String defining struct fields. */
  62. char *fields;
  63. /* NULL terminated array of strings defining base classes for a
  64. class. */
  65. char **baseclasses;
  66. /* String defining class methods. */
  67. char *methods;
  68. /* String defining vtable pointer for a class. */
  69. char *vtable;
  70. };
  71. /* This structure is used to keep track of type indices for tagged
  72. types. */
  73. struct stab_tag
  74. {
  75. /* The type index. */
  76. long index;
  77. /* The tag name. */
  78. const char *tag;
  79. /* The kind of type. This is set to DEBUG_KIND_ILLEGAL when the
  80. type is defined. */
  81. enum debug_type_kind kind;
  82. /* The size of the struct. */
  83. unsigned int size;
  84. };
  85. /* We remember various sorts of type indices. They are not related,
  86. but, for convenience, we keep all the information in this
  87. structure. */
  88. struct stab_type_cache
  89. {
  90. /* The void type index. */
  91. long void_type;
  92. /* Signed integer type indices, indexed by size - 1. */
  93. long signed_integer_types[8];
  94. /* Unsigned integer type indices, indexed by size - 1. */
  95. long unsigned_integer_types[8];
  96. /* Floating point types, indexed by size - 1. */
  97. long float_types[16];
  98. /* Pointers to types, indexed by the type index. */
  99. long *pointer_types;
  100. size_t pointer_types_alloc;
  101. /* Functions returning types, indexed by the type index. */
  102. long *function_types;
  103. size_t function_types_alloc;
  104. /* References to types, indexed by the type index. */
  105. long *reference_types;
  106. size_t reference_types_alloc;
  107. /* Struct/union/class type indices, indexed by the struct id. */
  108. struct stab_tag *struct_types;
  109. size_t struct_types_alloc;
  110. };
  111. /* This is the handle passed through debug_write. */
  112. struct stab_write_handle
  113. {
  114. /* The BFD. */
  115. bfd *abfd;
  116. /* This buffer holds the symbols. */
  117. bfd_byte *symbols;
  118. size_t symbols_size;
  119. size_t symbols_alloc;
  120. /* This is a list of hash table entries for the strings. */
  121. struct string_hash_entry *strings;
  122. /* The last string hash table entry. */
  123. struct string_hash_entry *last_string;
  124. /* The size of the strings. */
  125. size_t strings_size;
  126. /* This hash table eliminates duplicate strings. */
  127. struct string_hash_table strhash;
  128. /* The type stack. */
  129. struct stab_type_stack *type_stack;
  130. /* The next type index. */
  131. long type_index;
  132. /* The type cache. */
  133. struct stab_type_cache type_cache;
  134. /* A mapping from typedef names to type indices. */
  135. struct string_hash_table typedef_hash;
  136. /* If this is not -1, it is the offset to the most recent N_SO
  137. symbol, and the value of that symbol needs to be set. */
  138. long so_offset;
  139. /* If this is not -1, it is the offset to the most recent N_FUN
  140. symbol, and the value of that symbol needs to be set. */
  141. long fun_offset;
  142. /* The last text section address seen. */
  143. bfd_vma last_text_address;
  144. /* The block nesting depth. */
  145. unsigned int nesting;
  146. /* The function address. */
  147. bfd_vma fnaddr;
  148. /* A pending LBRAC symbol. */
  149. bfd_vma pending_lbrac;
  150. /* The current line number file name. */
  151. const char *lineno_filename;
  152. };
  153. static struct bfd_hash_entry *string_hash_newfunc
  154. (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
  155. static bfd_boolean stab_write_symbol
  156. (struct stab_write_handle *, int, int, bfd_vma, const char *);
  157. static bfd_boolean stab_push_string
  158. (struct stab_write_handle *, const char *, long, bfd_boolean, unsigned int);
  159. static bfd_boolean stab_push_defined_type
  160. (struct stab_write_handle *, long, unsigned int);
  161. static char *stab_pop_type (struct stab_write_handle *);
  162. static bfd_boolean stab_modify_type
  163. (struct stab_write_handle *, int, unsigned int, long **, size_t *);
  164. static long stab_get_struct_index
  165. (struct stab_write_handle *, const char *, unsigned int,
  166. enum debug_type_kind, unsigned int *);
  167. static bfd_boolean stab_class_method_var
  168. (struct stab_write_handle *, const char *, enum debug_visibility,
  169. bfd_boolean, bfd_boolean, bfd_boolean, bfd_vma, bfd_boolean);
  170. static bfd_boolean stab_start_compilation_unit (void *, const char *);
  171. static bfd_boolean stab_start_source (void *, const char *);
  172. static bfd_boolean stab_empty_type (void *);
  173. static bfd_boolean stab_void_type (void *);
  174. static bfd_boolean stab_int_type (void *, unsigned int, bfd_boolean);
  175. static bfd_boolean stab_float_type (void *, unsigned int);
  176. static bfd_boolean stab_complex_type (void *, unsigned int);
  177. static bfd_boolean stab_bool_type (void *, unsigned int);
  178. static bfd_boolean stab_enum_type
  179. (void *, const char *, const char **, bfd_signed_vma *);
  180. static bfd_boolean stab_pointer_type (void *);
  181. static bfd_boolean stab_function_type (void *, int, bfd_boolean);
  182. static bfd_boolean stab_reference_type (void *);
  183. static bfd_boolean stab_range_type (void *, bfd_signed_vma, bfd_signed_vma);
  184. static bfd_boolean stab_array_type
  185. (void *, bfd_signed_vma, bfd_signed_vma, bfd_boolean);
  186. static bfd_boolean stab_set_type (void *, bfd_boolean);
  187. static bfd_boolean stab_offset_type (void *);
  188. static bfd_boolean stab_method_type (void *, bfd_boolean, int, bfd_boolean);
  189. static bfd_boolean stab_const_type (void *);
  190. static bfd_boolean stab_volatile_type (void *);
  191. static bfd_boolean stab_start_struct_type
  192. (void *, const char *, unsigned int, bfd_boolean, unsigned int);
  193. static bfd_boolean stab_struct_field
  194. (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
  195. static bfd_boolean stab_end_struct_type (void *);
  196. static bfd_boolean stab_start_class_type
  197. (void *, const char *, unsigned int, bfd_boolean, unsigned int,
  198. bfd_boolean, bfd_boolean);
  199. static bfd_boolean stab_class_static_member
  200. (void *, const char *, const char *, enum debug_visibility);
  201. static bfd_boolean stab_class_baseclass
  202. (void *, bfd_vma, bfd_boolean, enum debug_visibility);
  203. static bfd_boolean stab_class_start_method (void *, const char *);
  204. static bfd_boolean stab_class_method_variant
  205. (void *, const char *, enum debug_visibility, bfd_boolean, bfd_boolean,
  206. bfd_vma, bfd_boolean);
  207. static bfd_boolean stab_class_static_method_variant
  208. (void *, const char *, enum debug_visibility, bfd_boolean, bfd_boolean);
  209. static bfd_boolean stab_class_end_method (void *);
  210. static bfd_boolean stab_end_class_type (void *);
  211. static bfd_boolean stab_typedef_type (void *, const char *);
  212. static bfd_boolean stab_tag_type
  213. (void *, const char *, unsigned int, enum debug_type_kind);
  214. static bfd_boolean stab_typdef (void *, const char *);
  215. static bfd_boolean stab_tag (void *, const char *);
  216. static bfd_boolean stab_int_constant (void *, const char *, bfd_vma);
  217. static bfd_boolean stab_float_constant (void *, const char *, double);
  218. static bfd_boolean stab_typed_constant (void *, const char *, bfd_vma);
  219. static bfd_boolean stab_variable
  220. (void *, const char *, enum debug_var_kind, bfd_vma);
  221. static bfd_boolean stab_start_function (void *, const char *, bfd_boolean);
  222. static bfd_boolean stab_function_parameter
  223. (void *, const char *, enum debug_parm_kind, bfd_vma);
  224. static bfd_boolean stab_start_block (void *, bfd_vma);
  225. static bfd_boolean stab_end_block (void *, bfd_vma);
  226. static bfd_boolean stab_end_function (void *);
  227. static bfd_boolean stab_lineno (void *, const char *, unsigned long, bfd_vma);
  228. static const struct debug_write_fns stab_fns =
  229. {
  230. stab_start_compilation_unit,
  231. stab_start_source,
  232. stab_empty_type,
  233. stab_void_type,
  234. stab_int_type,
  235. stab_float_type,
  236. stab_complex_type,
  237. stab_bool_type,
  238. stab_enum_type,
  239. stab_pointer_type,
  240. stab_function_type,
  241. stab_reference_type,
  242. stab_range_type,
  243. stab_array_type,
  244. stab_set_type,
  245. stab_offset_type,
  246. stab_method_type,
  247. stab_const_type,
  248. stab_volatile_type,
  249. stab_start_struct_type,
  250. stab_struct_field,
  251. stab_end_struct_type,
  252. stab_start_class_type,
  253. stab_class_static_member,
  254. stab_class_baseclass,
  255. stab_class_start_method,
  256. stab_class_method_variant,
  257. stab_class_static_method_variant,
  258. stab_class_end_method,
  259. stab_end_class_type,
  260. stab_typedef_type,
  261. stab_tag_type,
  262. stab_typdef,
  263. stab_tag,
  264. stab_int_constant,
  265. stab_float_constant,
  266. stab_typed_constant,
  267. stab_variable,
  268. stab_start_function,
  269. stab_function_parameter,
  270. stab_start_block,
  271. stab_end_block,
  272. stab_end_function,
  273. stab_lineno
  274. };
  275. /* Routine to create an entry in a string hash table. */
  276. static struct bfd_hash_entry *
  277. string_hash_newfunc (struct bfd_hash_entry *entry,
  278. struct bfd_hash_table *table, const char *string)
  279. {
  280. struct string_hash_entry *ret = (struct string_hash_entry *) entry;
  281. /* Allocate the structure if it has not already been allocated by a
  282. subclass. */
  283. if (ret == (struct string_hash_entry *) NULL)
  284. ret = ((struct string_hash_entry *)
  285. bfd_hash_allocate (table, sizeof (struct string_hash_entry)));
  286. if (ret == (struct string_hash_entry *) NULL)
  287. return NULL;
  288. /* Call the allocation method of the superclass. */
  289. ret = ((struct string_hash_entry *)
  290. bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
  291. if (ret)
  292. {
  293. /* Initialize the local fields. */
  294. ret->next = NULL;
  295. ret->index = -1;
  296. ret->size = 0;
  297. }
  298. return (struct bfd_hash_entry *) ret;
  299. }
  300. /* Look up an entry in a string hash table. */
  301. #define string_hash_lookup(t, string, create, copy) \
  302. ((struct string_hash_entry *) \
  303. bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
  304. /* Add a symbol to the stabs debugging information we are building. */
  305. static bfd_boolean
  306. stab_write_symbol (struct stab_write_handle *info, int type, int desc,
  307. bfd_vma value, const char *string)
  308. {
  309. bfd_size_type strx;
  310. bfd_byte sym[STAB_SYMBOL_SIZE];
  311. if (string == NULL)
  312. strx = 0;
  313. else
  314. {
  315. struct string_hash_entry *h;
  316. h = string_hash_lookup (&info->strhash, string, TRUE, TRUE);
  317. if (h == NULL)
  318. {
  319. non_fatal (_("string_hash_lookup failed: %s"),
  320. bfd_errmsg (bfd_get_error ()));
  321. return FALSE;
  322. }
  323. if (h->index != -1)
  324. strx = h->index;
  325. else
  326. {
  327. strx = info->strings_size;
  328. h->index = strx;
  329. if (info->last_string == NULL)
  330. info->strings = h;
  331. else
  332. info->last_string->next = h;
  333. info->last_string = h;
  334. info->strings_size += strlen (string) + 1;
  335. }
  336. }
  337. /* This presumes 32 bit values. */
  338. bfd_put_32 (info->abfd, strx, sym);
  339. bfd_put_8 (info->abfd, type, sym + 4);
  340. bfd_put_8 (info->abfd, 0, sym + 5);
  341. bfd_put_16 (info->abfd, desc, sym + 6);
  342. bfd_put_32 (info->abfd, value, sym + 8);
  343. if (info->symbols_size + STAB_SYMBOL_SIZE > info->symbols_alloc)
  344. {
  345. info->symbols_alloc *= 2;
  346. info->symbols = (bfd_byte *) xrealloc (info->symbols,
  347. info->symbols_alloc);
  348. }
  349. memcpy (info->symbols + info->symbols_size, sym, STAB_SYMBOL_SIZE);
  350. info->symbols_size += STAB_SYMBOL_SIZE;
  351. return TRUE;
  352. }
  353. /* Push a string on to the type stack. */
  354. static bfd_boolean
  355. stab_push_string (struct stab_write_handle *info, const char *string,
  356. long tindex, bfd_boolean definition, unsigned int size)
  357. {
  358. struct stab_type_stack *s;
  359. s = (struct stab_type_stack *) xmalloc (sizeof *s);
  360. s->string = xstrdup (string);
  361. s->index = tindex;
  362. s->definition = definition;
  363. s->size = size;
  364. s->fields = NULL;
  365. s->baseclasses = NULL;
  366. s->methods = NULL;
  367. s->vtable = NULL;
  368. s->next = info->type_stack;
  369. info->type_stack = s;
  370. return TRUE;
  371. }
  372. /* Push a type index which has already been defined. */
  373. static bfd_boolean
  374. stab_push_defined_type (struct stab_write_handle *info, long tindex,
  375. unsigned int size)
  376. {
  377. char buf[20];
  378. sprintf (buf, "%ld", tindex);
  379. return stab_push_string (info, buf, tindex, FALSE, size);
  380. }
  381. /* Pop a type off the type stack. The caller is responsible for
  382. freeing the string. */
  383. static char *
  384. stab_pop_type (struct stab_write_handle *info)
  385. {
  386. struct stab_type_stack *s;
  387. char *ret;
  388. s = info->type_stack;
  389. assert (s != NULL);
  390. info->type_stack = s->next;
  391. ret = s->string;
  392. free (s);
  393. return ret;
  394. }
  395. /* The general routine to write out stabs in sections debugging
  396. information. This accumulates the stabs symbols and the strings in
  397. two obstacks. We can't easily write out the information as we go
  398. along, because we need to know the section sizes before we can
  399. write out the section contents. ABFD is the BFD and DHANDLE is the
  400. handle for the debugging information. This sets *PSYMS to point to
  401. the symbols, *PSYMSIZE the size of the symbols, *PSTRINGS to the
  402. strings, and *PSTRINGSIZE to the size of the strings. */
  403. bfd_boolean
  404. write_stabs_in_sections_debugging_info (bfd *abfd, void *dhandle,
  405. bfd_byte **psyms,
  406. bfd_size_type *psymsize,
  407. bfd_byte **pstrings,
  408. bfd_size_type *pstringsize)
  409. {
  410. struct stab_write_handle info;
  411. struct string_hash_entry *h;
  412. bfd_byte *p;
  413. info.abfd = abfd;
  414. info.symbols_size = 0;
  415. info.symbols_alloc = 500;
  416. info.symbols = (bfd_byte *) xmalloc (info.symbols_alloc);
  417. info.strings = NULL;
  418. info.last_string = NULL;
  419. /* Reserve 1 byte for a null byte. */
  420. info.strings_size = 1;
  421. if (!bfd_hash_table_init (&info.strhash.table, string_hash_newfunc,
  422. sizeof (struct string_hash_entry))
  423. || !bfd_hash_table_init (&info.typedef_hash.table, string_hash_newfunc,
  424. sizeof (struct string_hash_entry)))
  425. {
  426. non_fatal ("bfd_hash_table_init_failed: %s",
  427. bfd_errmsg (bfd_get_error ()));
  428. return FALSE;
  429. }
  430. info.type_stack = NULL;
  431. info.type_index = 1;
  432. memset (&info.type_cache, 0, sizeof info.type_cache);
  433. info.so_offset = -1;
  434. info.fun_offset = -1;
  435. info.last_text_address = 0;
  436. info.nesting = 0;
  437. info.fnaddr = 0;
  438. info.pending_lbrac = (bfd_vma) -1;
  439. /* The initial symbol holds the string size. */
  440. if (! stab_write_symbol (&info, 0, 0, 0, (const char *) NULL))
  441. return FALSE;
  442. /* Output an initial N_SO symbol. */
  443. info.so_offset = info.symbols_size;
  444. if (! stab_write_symbol (&info, N_SO, 0, 0, bfd_get_filename (abfd)))
  445. return FALSE;
  446. if (! debug_write (dhandle, &stab_fns, (void *) &info))
  447. return FALSE;
  448. assert (info.pending_lbrac == (bfd_vma) -1);
  449. /* Output a trailing N_SO. */
  450. if (! stab_write_symbol (&info, N_SO, 0, info.last_text_address,
  451. (const char *) NULL))
  452. return FALSE;
  453. /* Put the string size in the initial symbol. */
  454. bfd_put_32 (abfd, info.strings_size, info.symbols + 8);
  455. *psyms = info.symbols;
  456. *psymsize = info.symbols_size;
  457. *pstringsize = info.strings_size;
  458. *pstrings = (bfd_byte *) xmalloc (info.strings_size);
  459. p = *pstrings;
  460. *p++ = '\0';
  461. for (h = info.strings; h != NULL; h = h->next)
  462. {
  463. strcpy ((char *) p, h->root.string);
  464. p += strlen ((char *) p) + 1;
  465. }
  466. return TRUE;
  467. }
  468. /* Start writing out information for a compilation unit. */
  469. static bfd_boolean
  470. stab_start_compilation_unit (void *p, const char *filename)
  471. {
  472. struct stab_write_handle *info = (struct stab_write_handle *) p;
  473. /* We would normally output an N_SO symbol here. However, that
  474. would force us to reset all of our type information. I think we
  475. will be better off just outputting an N_SOL symbol, and not
  476. worrying about splitting information between files. */
  477. info->lineno_filename = filename;
  478. return stab_write_symbol (info, N_SOL, 0, 0, filename);
  479. }
  480. /* Start writing out information for a particular source file. */
  481. static bfd_boolean
  482. stab_start_source (void *p, const char *filename)
  483. {
  484. struct stab_write_handle *info = (struct stab_write_handle *) p;
  485. /* FIXME: The symbol's value is supposed to be the text section
  486. address. However, we would have to fill it in later, and gdb
  487. doesn't care, so we don't bother with it. */
  488. info->lineno_filename = filename;
  489. return stab_write_symbol (info, N_SOL, 0, 0, filename);
  490. }
  491. /* Push an empty type. This shouldn't normally happen. We just use a
  492. void type. */
  493. static bfd_boolean
  494. stab_empty_type (void *p)
  495. {
  496. struct stab_write_handle *info = (struct stab_write_handle *) p;
  497. /* We don't call stab_void_type if the type is not yet defined,
  498. because that might screw up the typedef. */
  499. if (info->type_cache.void_type != 0)
  500. return stab_push_defined_type (info, info->type_cache.void_type, 0);
  501. else
  502. {
  503. long tindex;
  504. char buf[40];
  505. tindex = info->type_index;
  506. ++info->type_index;
  507. sprintf (buf, "%ld=%ld", tindex, tindex);
  508. return stab_push_string (info, buf, tindex, FALSE, 0);
  509. }
  510. }
  511. /* Push a void type. */
  512. static bfd_boolean
  513. stab_void_type (void *p)
  514. {
  515. struct stab_write_handle *info = (struct stab_write_handle *) p;
  516. if (info->type_cache.void_type != 0)
  517. return stab_push_defined_type (info, info->type_cache.void_type, 0);
  518. else
  519. {
  520. long tindex;
  521. char buf[40];
  522. tindex = info->type_index;
  523. ++info->type_index;
  524. info->type_cache.void_type = tindex;
  525. sprintf (buf, "%ld=%ld", tindex, tindex);
  526. return stab_push_string (info, buf, tindex, TRUE, 0);
  527. }
  528. }
  529. /* Push an integer type. */
  530. static bfd_boolean
  531. stab_int_type (void *p, unsigned int size, bfd_boolean unsignedp)
  532. {
  533. struct stab_write_handle *info = (struct stab_write_handle *) p;
  534. long *cache;
  535. if (size <= 0 || (size > sizeof (long) && size != 8))
  536. {
  537. non_fatal (_("stab_int_type: bad size %u"), size);
  538. return FALSE;
  539. }
  540. if (unsignedp)
  541. cache = info->type_cache.signed_integer_types;
  542. else
  543. cache = info->type_cache.unsigned_integer_types;
  544. if (cache[size - 1] != 0)
  545. return stab_push_defined_type (info, cache[size - 1], size);
  546. else
  547. {
  548. long tindex;
  549. char buf[100];
  550. tindex = info->type_index;
  551. ++info->type_index;
  552. cache[size - 1] = tindex;
  553. sprintf (buf, "%ld=r%ld;", tindex, tindex);
  554. if (unsignedp)
  555. {
  556. strcat (buf, "0;");
  557. if (size < sizeof (long))
  558. sprintf (buf + strlen (buf), "%ld;", ((long) 1 << (size * 8)) - 1);
  559. else if (size == sizeof (long))
  560. strcat (buf, "-1;");
  561. else if (size == 8)
  562. strcat (buf, "01777777777777777777777;");
  563. else
  564. abort ();
  565. }
  566. else
  567. {
  568. if (size <= sizeof (long))
  569. sprintf (buf + strlen (buf), "%ld;%ld;",
  570. (long) - ((unsigned long) 1 << (size * 8 - 1)),
  571. (long) (((unsigned long) 1 << (size * 8 - 1)) - 1));
  572. else if (size == 8)
  573. strcat (buf, "01000000000000000000000;0777777777777777777777;");
  574. else
  575. abort ();
  576. }
  577. return stab_push_string (info, buf, tindex, TRUE, size);
  578. }
  579. }
  580. /* Push a floating point type. */
  581. static bfd_boolean
  582. stab_float_type (void *p, unsigned int size)
  583. {
  584. struct stab_write_handle *info = (struct stab_write_handle *) p;
  585. if (size > 0
  586. && size - 1 < (sizeof info->type_cache.float_types
  587. / sizeof info->type_cache.float_types[0])
  588. && info->type_cache.float_types[size - 1] != 0)
  589. return stab_push_defined_type (info,
  590. info->type_cache.float_types[size - 1],
  591. size);
  592. else
  593. {
  594. long tindex;
  595. char *int_type;
  596. char buf[50];
  597. /* Floats are defined as a subrange of int. */
  598. if (! stab_int_type (info, 4, FALSE))
  599. return FALSE;
  600. int_type = stab_pop_type (info);
  601. tindex = info->type_index;
  602. ++info->type_index;
  603. if (size > 0
  604. && size - 1 < (sizeof info->type_cache.float_types
  605. / sizeof info->type_cache.float_types[0]))
  606. info->type_cache.float_types[size - 1] = tindex;
  607. sprintf (buf, "%ld=r%s;%u;0;", tindex, int_type, size);
  608. free (int_type);
  609. return stab_push_string (info, buf, tindex, TRUE, size);
  610. }
  611. }
  612. /* Push a complex type. */
  613. static bfd_boolean
  614. stab_complex_type (void *p, unsigned int size)
  615. {
  616. struct stab_write_handle *info = (struct stab_write_handle *) p;
  617. char buf[50];
  618. long tindex;
  619. tindex = info->type_index;
  620. ++info->type_index;
  621. sprintf (buf, "%ld=r%ld;%u;0;", tindex, tindex, size);
  622. return stab_push_string (info, buf, tindex, TRUE, size * 2);
  623. }
  624. /* Push a bfd_boolean type. We use an XCOFF predefined type, since gdb
  625. always recognizes them. */
  626. static bfd_boolean
  627. stab_bool_type (void *p, unsigned int size)
  628. {
  629. struct stab_write_handle *info = (struct stab_write_handle *) p;
  630. long tindex;
  631. switch (size)
  632. {
  633. case 1:
  634. tindex = -21;
  635. break;
  636. case 2:
  637. tindex = -22;
  638. break;
  639. default:
  640. case 4:
  641. tindex = -16;
  642. break;
  643. case 8:
  644. tindex = -33;
  645. break;
  646. }
  647. return stab_push_defined_type (info, tindex, size);
  648. }
  649. /* Push an enum type. */
  650. static bfd_boolean
  651. stab_enum_type (void *p, const char *tag, const char **names,
  652. bfd_signed_vma *vals)
  653. {
  654. struct stab_write_handle *info = (struct stab_write_handle *) p;
  655. size_t len;
  656. const char **pn;
  657. char *buf;
  658. long tindex = 0;
  659. bfd_signed_vma *pv;
  660. if (names == NULL)
  661. {
  662. assert (tag != NULL);
  663. buf = (char *) xmalloc (10 + strlen (tag));
  664. sprintf (buf, "xe%s:", tag);
  665. /* FIXME: The size is just a guess. */
  666. if (! stab_push_string (info, buf, 0, FALSE, 4))
  667. return FALSE;
  668. free (buf);
  669. return TRUE;
  670. }
  671. len = 10;
  672. if (tag != NULL)
  673. len += strlen (tag);
  674. for (pn = names; *pn != NULL; pn++)
  675. len += strlen (*pn) + 20;
  676. buf = (char *) xmalloc (len);
  677. if (tag == NULL)
  678. strcpy (buf, "e");
  679. else
  680. {
  681. tindex = info->type_index;
  682. ++info->type_index;
  683. sprintf (buf, "%s:T%ld=e", tag, tindex);
  684. }
  685. for (pn = names, pv = vals; *pn != NULL; pn++, pv++)
  686. sprintf (buf + strlen (buf), "%s:%ld,", *pn, (long) *pv);
  687. strcat (buf, ";");
  688. if (tag == NULL)
  689. {
  690. /* FIXME: The size is just a guess. */
  691. if (! stab_push_string (info, buf, 0, FALSE, 4))
  692. return FALSE;
  693. }
  694. else
  695. {
  696. /* FIXME: The size is just a guess. */
  697. if (! stab_write_symbol (info, N_LSYM, 0, 0, buf)
  698. || ! stab_push_defined_type (info, tindex, 4))
  699. return FALSE;
  700. }
  701. free (buf);
  702. return TRUE;
  703. }
  704. /* Push a modification of the top type on the stack. Cache the
  705. results in CACHE and CACHE_ALLOC. */
  706. static bfd_boolean
  707. stab_modify_type (struct stab_write_handle *info, int mod,
  708. unsigned int size, long **cache, size_t *cache_alloc)
  709. {
  710. long targindex;
  711. long tindex;
  712. char *s, *buf;
  713. assert (info->type_stack != NULL);
  714. targindex = info->type_stack->index;
  715. if (targindex <= 0
  716. || cache == NULL)
  717. {
  718. bfd_boolean definition;
  719. /* Either the target type has no index, or we aren't caching
  720. this modifier. Either way we have no way of recording the
  721. new type, so we don't bother to define one. */
  722. definition = info->type_stack->definition;
  723. s = stab_pop_type (info);
  724. buf = (char *) xmalloc (strlen (s) + 2);
  725. sprintf (buf, "%c%s", mod, s);
  726. free (s);
  727. if (! stab_push_string (info, buf, 0, definition, size))
  728. return FALSE;
  729. free (buf);
  730. }
  731. else
  732. {
  733. if ((size_t) targindex >= *cache_alloc)
  734. {
  735. size_t alloc;
  736. alloc = *cache_alloc;
  737. if (alloc == 0)
  738. alloc = 10;
  739. while ((size_t) targindex >= alloc)
  740. alloc *= 2;
  741. *cache = (long *) xrealloc (*cache, alloc * sizeof (long));
  742. memset (*cache + *cache_alloc, 0,
  743. (alloc - *cache_alloc) * sizeof (long));
  744. *cache_alloc = alloc;
  745. }
  746. tindex = (*cache)[targindex];
  747. if (tindex != 0 && ! info->type_stack->definition)
  748. {
  749. /* We have already defined a modification of this type, and
  750. the entry on the type stack is not a definition, so we
  751. can safely discard it (we may have a definition on the
  752. stack, even if we already defined a modification, if it
  753. is a struct which we did not define at the time it was
  754. referenced). */
  755. free (stab_pop_type (info));
  756. if (! stab_push_defined_type (info, tindex, size))
  757. return FALSE;
  758. }
  759. else
  760. {
  761. tindex = info->type_index;
  762. ++info->type_index;
  763. s = stab_pop_type (info);
  764. buf = (char *) xmalloc (strlen (s) + 20);
  765. sprintf (buf, "%ld=%c%s", tindex, mod, s);
  766. free (s);
  767. (*cache)[targindex] = tindex;
  768. if (! stab_push_string (info, buf, tindex, TRUE, size))
  769. return FALSE;
  770. free (buf);
  771. }
  772. }
  773. return TRUE;
  774. }
  775. /* Push a pointer type. */
  776. static bfd_boolean
  777. stab_pointer_type (void *p)
  778. {
  779. struct stab_write_handle *info = (struct stab_write_handle *) p;
  780. /* FIXME: The size should depend upon the architecture. */
  781. return stab_modify_type (info, '*', 4, &info->type_cache.pointer_types,
  782. &info->type_cache.pointer_types_alloc);
  783. }
  784. /* Push a function type. */
  785. static bfd_boolean
  786. stab_function_type (void *p, int argcount,
  787. bfd_boolean varargs ATTRIBUTE_UNUSED)
  788. {
  789. struct stab_write_handle *info = (struct stab_write_handle *) p;
  790. int i;
  791. /* We have no way to represent the argument types, so we just
  792. discard them. However, if they define new types, we must output
  793. them. We do this by producing empty typedefs. */
  794. for (i = 0; i < argcount; i++)
  795. {
  796. if (! info->type_stack->definition)
  797. free (stab_pop_type (info));
  798. else
  799. {
  800. char *s, *buf;
  801. s = stab_pop_type (info);
  802. buf = (char *) xmalloc (strlen (s) + 3);
  803. sprintf (buf, ":t%s", s);
  804. free (s);
  805. if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
  806. return FALSE;
  807. free (buf);
  808. }
  809. }
  810. return stab_modify_type (info, 'f', 0, &info->type_cache.function_types,
  811. &info->type_cache.function_types_alloc);
  812. }
  813. /* Push a reference type. */
  814. static bfd_boolean
  815. stab_reference_type (void *p)
  816. {
  817. struct stab_write_handle *info = (struct stab_write_handle *) p;
  818. /* FIXME: The size should depend upon the architecture. */
  819. return stab_modify_type (info, '&', 4, &info->type_cache.reference_types,
  820. &info->type_cache.reference_types_alloc);
  821. }
  822. /* Push a range type. */
  823. static bfd_boolean
  824. stab_range_type (void *p, bfd_signed_vma low, bfd_signed_vma high)
  825. {
  826. struct stab_write_handle *info = (struct stab_write_handle *) p;
  827. bfd_boolean definition;
  828. unsigned int size;
  829. char *s, *buf;
  830. definition = info->type_stack->definition;
  831. size = info->type_stack->size;
  832. s = stab_pop_type (info);
  833. buf = (char *) xmalloc (strlen (s) + 100);
  834. sprintf (buf, "r%s;%ld;%ld;", s, (long) low, (long) high);
  835. free (s);
  836. if (! stab_push_string (info, buf, 0, definition, size))
  837. return FALSE;
  838. free (buf);
  839. return TRUE;
  840. }
  841. /* Push an array type. */
  842. static bfd_boolean
  843. stab_array_type (void *p, bfd_signed_vma low, bfd_signed_vma high,
  844. bfd_boolean stringp)
  845. {
  846. struct stab_write_handle *info = (struct stab_write_handle *) p;
  847. bfd_boolean definition;
  848. unsigned int element_size;
  849. char *range, *element, *buf;
  850. long tindex;
  851. unsigned int size;
  852. definition = info->type_stack->definition;
  853. range = stab_pop_type (info);
  854. definition = definition || info->type_stack->definition;
  855. element_size = info->type_stack->size;
  856. element = stab_pop_type (info);
  857. buf = (char *) xmalloc (strlen (range) + strlen (element) + 100);
  858. if (! stringp)
  859. {
  860. tindex = 0;
  861. *buf = '\0';
  862. }
  863. else
  864. {
  865. /* We need to define a type in order to include the string
  866. attribute. */
  867. tindex = info->type_index;
  868. ++info->type_index;
  869. definition = TRUE;
  870. sprintf (buf, "%ld=@S;", tindex);
  871. }
  872. sprintf (buf + strlen (buf), "ar%s;%ld;%ld;%s",
  873. range, (long) low, (long) high, element);
  874. free (range);
  875. free (element);
  876. if (high < low)
  877. size = 0;
  878. else
  879. size = element_size * ((high - low) + 1);
  880. if (! stab_push_string (info, buf, tindex, definition, size))
  881. return FALSE;
  882. free (buf);
  883. return TRUE;
  884. }
  885. /* Push a set type. */
  886. static bfd_boolean
  887. stab_set_type (void *p, bfd_boolean bitstringp)
  888. {
  889. struct stab_write_handle *info = (struct stab_write_handle *) p;
  890. bfd_boolean definition;
  891. char *s, *buf;
  892. long tindex;
  893. definition = info->type_stack->definition;
  894. s = stab_pop_type (info);
  895. buf = (char *) xmalloc (strlen (s) + 30);
  896. if (! bitstringp)
  897. {
  898. *buf = '\0';
  899. tindex = 0;
  900. }
  901. else
  902. {
  903. /* We need to define a type in order to include the string
  904. attribute. */
  905. tindex = info->type_index;
  906. ++info->type_index;
  907. definition = TRUE;
  908. sprintf (buf, "%ld=@S;", tindex);
  909. }
  910. sprintf (buf + strlen (buf), "S%s", s);
  911. free (s);
  912. if (! stab_push_string (info, buf, tindex, definition, 0))
  913. return FALSE;
  914. free (buf);
  915. return TRUE;
  916. }
  917. /* Push an offset type. */
  918. static bfd_boolean
  919. stab_offset_type (void *p)
  920. {
  921. struct stab_write_handle *info = (struct stab_write_handle *) p;
  922. bfd_boolean definition;
  923. char *target, *base, *buf;
  924. definition = info->type_stack->definition;
  925. target = stab_pop_type (info);
  926. definition = definition || info->type_stack->definition;
  927. base = stab_pop_type (info);
  928. buf = (char *) xmalloc (strlen (target) + strlen (base) + 3);
  929. sprintf (buf, "@%s,%s", base, target);
  930. free (base);
  931. free (target);
  932. if (! stab_push_string (info, buf, 0, definition, 0))
  933. return FALSE;
  934. free (buf);
  935. return TRUE;
  936. }
  937. /* Push a method type. */
  938. static bfd_boolean
  939. stab_method_type (void *p, bfd_boolean domainp, int argcount,
  940. bfd_boolean varargs)
  941. {
  942. struct stab_write_handle *info = (struct stab_write_handle *) p;
  943. bfd_boolean definition;
  944. char *domain, *return_type, *buf;
  945. char **args;
  946. int i;
  947. size_t len;
  948. /* We don't bother with stub method types, because that would
  949. require a mangler for C++ argument types. This will waste space
  950. in the debugging output. */
  951. /* We need a domain. I'm not sure DOMAINP can ever be false,
  952. anyhow. */
  953. if (! domainp)
  954. {
  955. if (! stab_empty_type (p))
  956. return FALSE;
  957. }
  958. definition = info->type_stack->definition;
  959. domain = stab_pop_type (info);
  960. /* A non-varargs function is indicated by making the last parameter
  961. type be void. */
  962. if (argcount < 0)
  963. {
  964. args = NULL;
  965. argcount = 0;
  966. }
  967. else if (argcount == 0)
  968. {
  969. if (varargs)
  970. args = NULL;
  971. else
  972. {
  973. args = (char **) xmalloc (1 * sizeof (*args));
  974. if (! stab_empty_type (p))
  975. return FALSE;
  976. definition = definition || info->type_stack->definition;
  977. args[0] = stab_pop_type (info);
  978. argcount = 1;
  979. }
  980. }
  981. else
  982. {
  983. args = (char **) xmalloc ((argcount + 1) * sizeof (*args));
  984. for (i = argcount - 1; i >= 0; i--)
  985. {
  986. definition = definition || info->type_stack->definition;
  987. args[i] = stab_pop_type (info);
  988. }
  989. if (! varargs)
  990. {
  991. if (! stab_empty_type (p))
  992. return FALSE;
  993. definition = definition || info->type_stack->definition;
  994. args[argcount] = stab_pop_type (info);
  995. ++argcount;
  996. }
  997. }
  998. definition = definition || info->type_stack->definition;
  999. return_type = stab_pop_type (info);
  1000. len = strlen (domain) + strlen (return_type) + 10;
  1001. for (i = 0; i < argcount; i++)
  1002. len += strlen (args[i]);
  1003. buf = (char *) xmalloc (len);
  1004. sprintf (buf, "#%s,%s", domain, return_type);
  1005. free (domain);
  1006. free (return_type);
  1007. for (i = 0; i < argcount; i++)
  1008. {
  1009. strcat (buf, ",");
  1010. strcat (buf, args[i]);
  1011. free (args[i]);
  1012. }
  1013. strcat (buf, ";");
  1014. if (args != NULL)
  1015. free (args);
  1016. if (! stab_push_string (info, buf, 0, definition, 0))
  1017. return FALSE;
  1018. free (buf);
  1019. return TRUE;
  1020. }
  1021. /* Push a const version of a type. */
  1022. static bfd_boolean
  1023. stab_const_type (void *p)
  1024. {
  1025. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1026. return stab_modify_type (info, 'k', info->type_stack->size,
  1027. (long **) NULL, (size_t *) NULL);
  1028. }
  1029. /* Push a volatile version of a type. */
  1030. static bfd_boolean
  1031. stab_volatile_type (void *p)
  1032. {
  1033. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1034. return stab_modify_type (info, 'B', info->type_stack->size,
  1035. (long **) NULL, (size_t *) NULL);
  1036. }
  1037. /* Get the type index to use for a struct/union/class ID. This should
  1038. return -1 if it fails. */
  1039. static long
  1040. stab_get_struct_index (struct stab_write_handle *info, const char *tag,
  1041. unsigned int id, enum debug_type_kind kind,
  1042. unsigned int *psize)
  1043. {
  1044. if (id >= info->type_cache.struct_types_alloc)
  1045. {
  1046. size_t alloc;
  1047. alloc = info->type_cache.struct_types_alloc;
  1048. if (alloc == 0)
  1049. alloc = 10;
  1050. while (id >= alloc)
  1051. alloc *= 2;
  1052. info->type_cache.struct_types =
  1053. (struct stab_tag *) xrealloc (info->type_cache.struct_types,
  1054. alloc * sizeof (struct stab_tag));
  1055. memset ((info->type_cache.struct_types
  1056. + info->type_cache.struct_types_alloc),
  1057. 0,
  1058. ((alloc - info->type_cache.struct_types_alloc)
  1059. * sizeof (struct stab_tag)));
  1060. info->type_cache.struct_types_alloc = alloc;
  1061. }
  1062. if (info->type_cache.struct_types[id].index == 0)
  1063. {
  1064. info->type_cache.struct_types[id].index = info->type_index;
  1065. ++info->type_index;
  1066. info->type_cache.struct_types[id].tag = tag;
  1067. info->type_cache.struct_types[id].kind = kind;
  1068. }
  1069. if (kind == DEBUG_KIND_ILLEGAL)
  1070. {
  1071. /* This is a definition of the struct. */
  1072. info->type_cache.struct_types[id].kind = kind;
  1073. info->type_cache.struct_types[id].size = *psize;
  1074. }
  1075. else
  1076. *psize = info->type_cache.struct_types[id].size;
  1077. return info->type_cache.struct_types[id].index;
  1078. }
  1079. /* Start outputting a struct. We ignore the tag, and handle it in
  1080. stab_tag. */
  1081. static bfd_boolean
  1082. stab_start_struct_type (void *p, const char *tag, unsigned int id,
  1083. bfd_boolean structp, unsigned int size)
  1084. {
  1085. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1086. long tindex;
  1087. bfd_boolean definition;
  1088. char buf[40];
  1089. if (id == 0)
  1090. {
  1091. tindex = 0;
  1092. *buf = '\0';
  1093. definition = FALSE;
  1094. }
  1095. else
  1096. {
  1097. tindex = stab_get_struct_index (info, tag, id, DEBUG_KIND_ILLEGAL,
  1098. &size);
  1099. if (tindex < 0)
  1100. return FALSE;
  1101. sprintf (buf, "%ld=", tindex);
  1102. definition = TRUE;
  1103. }
  1104. sprintf (buf + strlen (buf), "%c%u",
  1105. structp ? 's' : 'u',
  1106. size);
  1107. if (! stab_push_string (info, buf, tindex, definition, size))
  1108. return FALSE;
  1109. info->type_stack->fields = (char *) xmalloc (1);
  1110. info->type_stack->fields[0] = '\0';
  1111. return TRUE;
  1112. }
  1113. /* Add a field to a struct. */
  1114. static bfd_boolean
  1115. stab_struct_field (void *p, const char *name, bfd_vma bitpos,
  1116. bfd_vma bitsize, enum debug_visibility visibility)
  1117. {
  1118. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1119. bfd_boolean definition;
  1120. unsigned int size;
  1121. char *s, *n;
  1122. const char *vis;
  1123. definition = info->type_stack->definition;
  1124. size = info->type_stack->size;
  1125. s = stab_pop_type (info);
  1126. /* Add this field to the end of the current struct fields, which is
  1127. currently on the top of the stack. */
  1128. assert (info->type_stack->fields != NULL);
  1129. n = (char *) xmalloc (strlen (info->type_stack->fields)
  1130. + strlen (name)
  1131. + strlen (s)
  1132. + 50);
  1133. switch (visibility)
  1134. {
  1135. default:
  1136. abort ();
  1137. case DEBUG_VISIBILITY_PUBLIC:
  1138. vis = "";
  1139. break;
  1140. case DEBUG_VISIBILITY_PRIVATE:
  1141. vis = "/0";
  1142. break;
  1143. case DEBUG_VISIBILITY_PROTECTED:
  1144. vis = "/1";
  1145. break;
  1146. }
  1147. if (bitsize == 0)
  1148. {
  1149. bitsize = size * 8;
  1150. if (bitsize == 0)
  1151. non_fatal (_("%s: warning: unknown size for field `%s' in struct"),
  1152. bfd_get_filename (info->abfd), name);
  1153. }
  1154. sprintf (n, "%s%s:%s%s,%ld,%ld;", info->type_stack->fields, name, vis, s,
  1155. (long) bitpos, (long) bitsize);
  1156. free (info->type_stack->fields);
  1157. info->type_stack->fields = n;
  1158. if (definition)
  1159. info->type_stack->definition = TRUE;
  1160. return TRUE;
  1161. }
  1162. /* Finish up a struct. */
  1163. static bfd_boolean
  1164. stab_end_struct_type (void *p)
  1165. {
  1166. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1167. bfd_boolean definition;
  1168. long tindex;
  1169. unsigned int size;
  1170. char *fields, *first, *buf;
  1171. assert (info->type_stack != NULL && info->type_stack->fields != NULL);
  1172. definition = info->type_stack->definition;
  1173. tindex = info->type_stack->index;
  1174. size = info->type_stack->size;
  1175. fields = info->type_stack->fields;
  1176. first = stab_pop_type (info);
  1177. buf = (char *) xmalloc (strlen (first) + strlen (fields) + 2);
  1178. sprintf (buf, "%s%s;", first, fields);
  1179. free (first);
  1180. free (fields);
  1181. if (! stab_push_string (info, buf, tindex, definition, size))
  1182. return FALSE;
  1183. free (buf);
  1184. return TRUE;
  1185. }
  1186. /* Start outputting a class. */
  1187. static bfd_boolean
  1188. stab_start_class_type (void *p, const char *tag, unsigned int id, bfd_boolean structp, unsigned int size, bfd_boolean vptr, bfd_boolean ownvptr)
  1189. {
  1190. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1191. bfd_boolean definition;
  1192. char *vstring;
  1193. if (! vptr || ownvptr)
  1194. {
  1195. definition = FALSE;
  1196. vstring = NULL;
  1197. }
  1198. else
  1199. {
  1200. definition = info->type_stack->definition;
  1201. vstring = stab_pop_type (info);
  1202. }
  1203. if (! stab_start_struct_type (p, tag, id, structp, size))
  1204. return FALSE;
  1205. if (vptr)
  1206. {
  1207. char *vtable;
  1208. if (ownvptr)
  1209. {
  1210. assert (info->type_stack->index > 0);
  1211. vtable = (char *) xmalloc (20);
  1212. sprintf (vtable, "~%%%ld", info->type_stack->index);
  1213. }
  1214. else
  1215. {
  1216. vtable = (char *) xmalloc (strlen (vstring) + 3);
  1217. sprintf (vtable, "~%%%s", vstring);
  1218. free (vstring);
  1219. }
  1220. info->type_stack->vtable = vtable;
  1221. }
  1222. if (definition)
  1223. info->type_stack->definition = TRUE;
  1224. return TRUE;
  1225. }
  1226. /* Add a static member to the class on the type stack. */
  1227. static bfd_boolean
  1228. stab_class_static_member (void *p, const char *name, const char *physname,
  1229. enum debug_visibility visibility)
  1230. {
  1231. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1232. bfd_boolean definition;
  1233. char *s, *n;
  1234. const char *vis;
  1235. definition = info->type_stack->definition;
  1236. s = stab_pop_type (info);
  1237. /* Add this field to the end of the current struct fields, which is
  1238. currently on the top of the stack. */
  1239. assert (info->type_stack->fields != NULL);
  1240. n = (char *) xmalloc (strlen (info->type_stack->fields)
  1241. + strlen (name)
  1242. + strlen (s)
  1243. + strlen (physname)
  1244. + 10);
  1245. switch (visibility)
  1246. {
  1247. default:
  1248. abort ();
  1249. case DEBUG_VISIBILITY_PUBLIC:
  1250. vis = "";
  1251. break;
  1252. case DEBUG_VISIBILITY_PRIVATE:
  1253. vis = "/0";
  1254. break;
  1255. case DEBUG_VISIBILITY_PROTECTED:
  1256. vis = "/1";
  1257. break;
  1258. }
  1259. sprintf (n, "%s%s:%s%s:%s;", info->type_stack->fields, name, vis, s,
  1260. physname);
  1261. free (info->type_stack->fields);
  1262. info->type_stack->fields = n;
  1263. if (definition)
  1264. info->type_stack->definition = TRUE;
  1265. return TRUE;
  1266. }
  1267. /* Add a base class to the class on the type stack. */
  1268. static bfd_boolean
  1269. stab_class_baseclass (void *p, bfd_vma bitpos, bfd_boolean is_virtual,
  1270. enum debug_visibility visibility)
  1271. {
  1272. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1273. bfd_boolean definition;
  1274. char *s;
  1275. char *buf;
  1276. unsigned int c;
  1277. char **baseclasses;
  1278. definition = info->type_stack->definition;
  1279. s = stab_pop_type (info);
  1280. /* Build the base class specifier. */
  1281. buf = (char *) xmalloc (strlen (s) + 25);
  1282. buf[0] = is_virtual ? '1' : '0';
  1283. switch (visibility)
  1284. {
  1285. default:
  1286. abort ();
  1287. case DEBUG_VISIBILITY_PRIVATE:
  1288. buf[1] = '0';
  1289. break;
  1290. case DEBUG_VISIBILITY_PROTECTED:
  1291. buf[1] = '1';
  1292. break;
  1293. case DEBUG_VISIBILITY_PUBLIC:
  1294. buf[1] = '2';
  1295. break;
  1296. }
  1297. sprintf (buf + 2, "%ld,%s;", (long) bitpos, s);
  1298. free (s);
  1299. /* Add the new baseclass to the existing ones. */
  1300. assert (info->type_stack != NULL && info->type_stack->fields != NULL);
  1301. if (info->type_stack->baseclasses == NULL)
  1302. c = 0;
  1303. else
  1304. {
  1305. c = 0;
  1306. while (info->type_stack->baseclasses[c] != NULL)
  1307. ++c;
  1308. }
  1309. baseclasses = (char **) xrealloc (info->type_stack->baseclasses,
  1310. (c + 2) * sizeof (*baseclasses));
  1311. baseclasses[c] = buf;
  1312. baseclasses[c + 1] = NULL;
  1313. info->type_stack->baseclasses = baseclasses;
  1314. if (definition)
  1315. info->type_stack->definition = TRUE;
  1316. return TRUE;
  1317. }
  1318. /* Start adding a method to the class on the type stack. */
  1319. static bfd_boolean
  1320. stab_class_start_method (void *p, const char *name)
  1321. {
  1322. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1323. char *m;
  1324. assert (info->type_stack != NULL && info->type_stack->fields != NULL);
  1325. if (info->type_stack->methods == NULL)
  1326. {
  1327. m = (char *) xmalloc (strlen (name) + 3);
  1328. *m = '\0';
  1329. }
  1330. else
  1331. {
  1332. m = (char *) xrealloc (info->type_stack->methods,
  1333. (strlen (info->type_stack->methods)
  1334. + strlen (name)
  1335. + 4));
  1336. }
  1337. sprintf (m + strlen (m), "%s::", name);
  1338. info->type_stack->methods = m;
  1339. return TRUE;
  1340. }
  1341. /* Add a variant, either static or not, to the current method. */
  1342. static bfd_boolean
  1343. stab_class_method_var (struct stab_write_handle *info, const char *physname,
  1344. enum debug_visibility visibility,
  1345. bfd_boolean staticp, bfd_boolean constp,
  1346. bfd_boolean volatilep, bfd_vma voffset,
  1347. bfd_boolean contextp)
  1348. {
  1349. bfd_boolean definition;
  1350. char *type;
  1351. char *context = NULL;
  1352. char visc, qualc, typec;
  1353. definition = info->type_stack->definition;
  1354. type = stab_pop_type (info);
  1355. if (contextp)
  1356. {
  1357. definition = definition || info->type_stack->definition;
  1358. context = stab_pop_type (info);
  1359. }
  1360. assert (info->type_stack != NULL && info->type_stack->methods != NULL);
  1361. switch (visibility)
  1362. {
  1363. default:
  1364. abort ();
  1365. case DEBUG_VISIBILITY_PRIVATE:
  1366. visc = '0';
  1367. break;
  1368. case DEBUG_VISIBILITY_PROTECTED:
  1369. visc = '1';
  1370. break;
  1371. case DEBUG_VISIBILITY_PUBLIC:
  1372. visc = '2';
  1373. break;
  1374. }
  1375. if (constp)
  1376. {
  1377. if (volatilep)
  1378. qualc = 'D';
  1379. else
  1380. qualc = 'B';
  1381. }
  1382. else
  1383. {
  1384. if (volatilep)
  1385. qualc = 'C';
  1386. else
  1387. qualc = 'A';
  1388. }
  1389. if (staticp)
  1390. typec = '?';
  1391. else if (! contextp)
  1392. typec = '.';
  1393. else
  1394. typec = '*';
  1395. info->type_stack->methods =
  1396. (char *) xrealloc (info->type_stack->methods,
  1397. (strlen (info->type_stack->methods)
  1398. + strlen (type)
  1399. + strlen (physname)
  1400. + (contextp ? strlen (context) : 0)
  1401. + 40));
  1402. sprintf (info->type_stack->methods + strlen (info->type_stack->methods),
  1403. "%s:%s;%c%c%c", type, physname, visc, qualc, typec);
  1404. free (type);
  1405. if (contextp)
  1406. {
  1407. sprintf (info->type_stack->methods + strlen (info->type_stack->methods),
  1408. "%ld;%s;", (long) voffset, context);
  1409. free (context);
  1410. }
  1411. if (definition)
  1412. info->type_stack->definition = TRUE;
  1413. return TRUE;
  1414. }
  1415. /* Add a variant to the current method. */
  1416. static bfd_boolean
  1417. stab_class_method_variant (void *p, const char *physname,
  1418. enum debug_visibility visibility,
  1419. bfd_boolean constp, bfd_boolean volatilep,
  1420. bfd_vma voffset, bfd_boolean contextp)
  1421. {
  1422. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1423. return stab_class_method_var (info, physname, visibility, FALSE, constp,
  1424. volatilep, voffset, contextp);
  1425. }
  1426. /* Add a static variant to the current method. */
  1427. static bfd_boolean
  1428. stab_class_static_method_variant (void *p, const char *physname,
  1429. enum debug_visibility visibility,
  1430. bfd_boolean constp, bfd_boolean volatilep)
  1431. {
  1432. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1433. return stab_class_method_var (info, physname, visibility, TRUE, constp,
  1434. volatilep, 0, FALSE);
  1435. }
  1436. /* Finish up a method. */
  1437. static bfd_boolean
  1438. stab_class_end_method (void *p)
  1439. {
  1440. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1441. assert (info->type_stack != NULL && info->type_stack->methods != NULL);
  1442. /* We allocated enough room on info->type_stack->methods to add the
  1443. trailing semicolon. */
  1444. strcat (info->type_stack->methods, ";");
  1445. return TRUE;
  1446. }
  1447. /* Finish up a class. */
  1448. static bfd_boolean
  1449. stab_end_class_type (void *p)
  1450. {
  1451. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1452. size_t len;
  1453. unsigned int i = 0;
  1454. char *buf;
  1455. assert (info->type_stack != NULL && info->type_stack->fields != NULL);
  1456. /* Work out the size we need to allocate for the class definition. */
  1457. len = (strlen (info->type_stack->string)
  1458. + strlen (info->type_stack->fields)
  1459. + 10);
  1460. if (info->type_stack->baseclasses != NULL)
  1461. {
  1462. len += 20;
  1463. for (i = 0; info->type_stack->baseclasses[i] != NULL; i++)
  1464. len += strlen (info->type_stack->baseclasses[i]);
  1465. }
  1466. if (info->type_stack->methods != NULL)
  1467. len += strlen (info->type_stack->methods);
  1468. if (info->type_stack->vtable != NULL)
  1469. len += strlen (info->type_stack->vtable);
  1470. /* Build the class definition. */
  1471. buf = (char *) xmalloc (len);
  1472. strcpy (buf, info->type_stack->string);
  1473. if (info->type_stack->baseclasses != NULL)
  1474. {
  1475. sprintf (buf + strlen (buf), "!%u,", i);
  1476. for (i = 0; info->type_stack->baseclasses[i] != NULL; i++)
  1477. {
  1478. strcat (buf, info->type_stack->baseclasses[i]);
  1479. free (info->type_stack->baseclasses[i]);
  1480. }
  1481. free (info->type_stack->baseclasses);
  1482. info->type_stack->baseclasses = NULL;
  1483. }
  1484. strcat (buf, info->type_stack->fields);
  1485. free (info->type_stack->fields);
  1486. info->type_stack->fields = NULL;
  1487. if (info->type_stack->methods != NULL)
  1488. {
  1489. strcat (buf, info->type_stack->methods);
  1490. free (info->type_stack->methods);
  1491. info->type_stack->methods = NULL;
  1492. }
  1493. strcat (buf, ";");
  1494. if (info->type_stack->vtable != NULL)
  1495. {
  1496. strcat (buf, info->type_stack->vtable);
  1497. free (info->type_stack->vtable);
  1498. info->type_stack->vtable = NULL;
  1499. }
  1500. /* Replace the string on the top of the stack with the complete
  1501. class definition. */
  1502. free (info->type_stack->string);
  1503. info->type_stack->string = buf;
  1504. return TRUE;
  1505. }
  1506. /* Push a typedef which was previously defined. */
  1507. static bfd_boolean
  1508. stab_typedef_type (void *p, const char *name)
  1509. {
  1510. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1511. struct string_hash_entry *h;
  1512. h = string_hash_lookup (&info->typedef_hash, name, FALSE, FALSE);
  1513. assert (h != NULL && h->index > 0);
  1514. return stab_push_defined_type (info, h->index, h->size);
  1515. }
  1516. /* Push a struct, union or class tag. */
  1517. static bfd_boolean
  1518. stab_tag_type (void *p, const char *name, unsigned int id,
  1519. enum debug_type_kind kind)
  1520. {
  1521. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1522. long tindex;
  1523. unsigned int size = 0;
  1524. tindex = stab_get_struct_index (info, name, id, kind, &size);
  1525. if (tindex < 0)
  1526. return FALSE;
  1527. return stab_push_defined_type (info, tindex, size);
  1528. }
  1529. /* Define a typedef. */
  1530. static bfd_boolean
  1531. stab_typdef (void *p, const char *name)
  1532. {
  1533. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1534. long tindex;
  1535. unsigned int size;
  1536. char *s, *buf;
  1537. struct string_hash_entry *h;
  1538. tindex = info->type_stack->index;
  1539. size = info->type_stack->size;
  1540. s = stab_pop_type (info);
  1541. buf = (char *) xmalloc (strlen (name) + strlen (s) + 20);
  1542. if (tindex > 0)
  1543. sprintf (buf, "%s:t%s", name, s);
  1544. else
  1545. {
  1546. tindex = info->type_index;
  1547. ++info->type_index;
  1548. sprintf (buf, "%s:t%ld=%s", name, tindex, s);
  1549. }
  1550. free (s);
  1551. if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
  1552. return FALSE;
  1553. free (buf);
  1554. h = string_hash_lookup (&info->typedef_hash, name, TRUE, FALSE);
  1555. if (h == NULL)
  1556. {
  1557. non_fatal (_("string_hash_lookup failed: %s"),
  1558. bfd_errmsg (bfd_get_error ()));
  1559. return FALSE;
  1560. }
  1561. /* I don't think we care about redefinitions. */
  1562. h->index = tindex;
  1563. h->size = size;
  1564. return TRUE;
  1565. }
  1566. /* Define a tag. */
  1567. static bfd_boolean
  1568. stab_tag (void *p, const char *tag)
  1569. {
  1570. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1571. char *s, *buf;
  1572. s = stab_pop_type (info);
  1573. buf = (char *) xmalloc (strlen (tag) + strlen (s) + 3);
  1574. sprintf (buf, "%s:T%s", tag, s);
  1575. free (s);
  1576. if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
  1577. return FALSE;
  1578. free (buf);
  1579. return TRUE;
  1580. }
  1581. /* Define an integer constant. */
  1582. static bfd_boolean
  1583. stab_int_constant (void *p, const char *name, bfd_vma val)
  1584. {
  1585. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1586. char *buf;
  1587. buf = (char *) xmalloc (strlen (name) + 20);
  1588. sprintf (buf, "%s:c=i%ld", name, (long) val);
  1589. if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
  1590. return FALSE;
  1591. free (buf);
  1592. return TRUE;
  1593. }
  1594. /* Define a floating point constant. */
  1595. static bfd_boolean
  1596. stab_float_constant (void *p, const char *name, double val)
  1597. {
  1598. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1599. char *buf;
  1600. buf = (char *) xmalloc (strlen (name) + 20);
  1601. sprintf (buf, "%s:c=f%g", name, val);
  1602. if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
  1603. return FALSE;
  1604. free (buf);
  1605. return TRUE;
  1606. }
  1607. /* Define a typed constant. */
  1608. static bfd_boolean
  1609. stab_typed_constant (void *p, const char *name, bfd_vma val)
  1610. {
  1611. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1612. char *s, *buf;
  1613. s = stab_pop_type (info);
  1614. buf = (char *) xmalloc (strlen (name) + strlen (s) + 20);
  1615. sprintf (buf, "%s:c=e%s,%ld", name, s, (long) val);
  1616. free (s);
  1617. if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
  1618. return FALSE;
  1619. free (buf);
  1620. return TRUE;
  1621. }
  1622. /* Record a variable. */
  1623. static bfd_boolean
  1624. stab_variable (void *p, const char *name, enum debug_var_kind kind,
  1625. bfd_vma val)
  1626. {
  1627. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1628. char *s, *buf;
  1629. int stab_type;
  1630. const char *kindstr;
  1631. s = stab_pop_type (info);
  1632. switch (kind)
  1633. {
  1634. default:
  1635. abort ();
  1636. case DEBUG_GLOBAL:
  1637. stab_type = N_GSYM;
  1638. kindstr = "G";
  1639. break;
  1640. case DEBUG_STATIC:
  1641. stab_type = N_STSYM;
  1642. kindstr = "S";
  1643. break;
  1644. case DEBUG_LOCAL_STATIC:
  1645. stab_type = N_STSYM;
  1646. kindstr = "V";
  1647. break;
  1648. case DEBUG_LOCAL:
  1649. stab_type = N_LSYM;
  1650. kindstr = "";
  1651. /* Make sure that this is a type reference or definition. */
  1652. if (! ISDIGIT (*s))
  1653. {
  1654. char *n;
  1655. long tindex;
  1656. tindex = info->type_index;
  1657. ++info->type_index;
  1658. n = (char *) xmalloc (strlen (s) + 20);
  1659. sprintf (n, "%ld=%s", tindex, s);
  1660. free (s);
  1661. s = n;
  1662. }
  1663. break;
  1664. case DEBUG_REGISTER:
  1665. stab_type = N_RSYM;
  1666. kindstr = "r";
  1667. break;
  1668. }
  1669. buf = (char *) xmalloc (strlen (name) + strlen (s) + 3);
  1670. sprintf (buf, "%s:%s%s", name, kindstr, s);
  1671. free (s);
  1672. if (! stab_write_symbol (info, stab_type, 0, val, buf))
  1673. return FALSE;
  1674. free (buf);
  1675. return TRUE;
  1676. }
  1677. /* Start outputting a function. */
  1678. static bfd_boolean
  1679. stab_start_function (void *p, const char *name, bfd_boolean globalp)
  1680. {
  1681. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1682. char *rettype, *buf;
  1683. assert (info->nesting == 0 && info->fun_offset == -1);
  1684. rettype = stab_pop_type (info);
  1685. buf = (char *) xmalloc (strlen (name) + strlen (rettype) + 3);
  1686. sprintf (buf, "%s:%c%s", name,
  1687. globalp ? 'F' : 'f',
  1688. rettype);
  1689. /* We don't know the value now, so we set it in start_block. */
  1690. info->fun_offset = info->symbols_size;
  1691. if (! stab_write_symbol (info, N_FUN, 0, 0, buf))
  1692. return FALSE;
  1693. free (buf);
  1694. return TRUE;
  1695. }
  1696. /* Output a function parameter. */
  1697. static bfd_boolean
  1698. stab_function_parameter (void *p, const char *name, enum debug_parm_kind kind, bfd_vma val)
  1699. {
  1700. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1701. char *s, *buf;
  1702. int stab_type;
  1703. char kindc;
  1704. s = stab_pop_type (info);
  1705. switch (kind)
  1706. {
  1707. default:
  1708. abort ();
  1709. case DEBUG_PARM_STACK:
  1710. stab_type = N_PSYM;
  1711. kindc = 'p';
  1712. break;
  1713. case DEBUG_PARM_REG:
  1714. stab_type = N_RSYM;
  1715. kindc = 'P';
  1716. break;
  1717. case DEBUG_PARM_REFERENCE:
  1718. stab_type = N_PSYM;
  1719. kindc = 'v';
  1720. break;
  1721. case DEBUG_PARM_REF_REG:
  1722. stab_type = N_RSYM;
  1723. kindc = 'a';
  1724. break;
  1725. }
  1726. buf = (char *) xmalloc (strlen (name) + strlen (s) + 3);
  1727. sprintf (buf, "%s:%c%s", name, kindc, s);
  1728. free (s);
  1729. if (! stab_write_symbol (info, stab_type, 0, val, buf))
  1730. return FALSE;
  1731. free (buf);
  1732. return TRUE;
  1733. }
  1734. /* Start a block. */
  1735. static bfd_boolean
  1736. stab_start_block (void *p, bfd_vma addr)
  1737. {
  1738. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1739. /* Fill in any slots which have been waiting for the first known
  1740. text address. */
  1741. if (info->so_offset != -1)
  1742. {
  1743. bfd_put_32 (info->abfd, addr, info->symbols + info->so_offset + 8);
  1744. info->so_offset = -1;
  1745. }
  1746. if (info->fun_offset != -1)
  1747. {
  1748. bfd_put_32 (info->abfd, addr, info->symbols + info->fun_offset + 8);
  1749. info->fun_offset = -1;
  1750. }
  1751. ++info->nesting;
  1752. /* We will be called with a top level block surrounding the
  1753. function, but stabs information does not output that block, so we
  1754. ignore it. */
  1755. if (info->nesting == 1)
  1756. {
  1757. info->fnaddr = addr;
  1758. return TRUE;
  1759. }
  1760. /* We have to output the LBRAC symbol after any variables which are
  1761. declared inside the block. We postpone the LBRAC until the next
  1762. start_block or end_block. */
  1763. /* If we have postponed an LBRAC, output it now. */
  1764. if (info->pending_lbrac != (bfd_vma) -1)
  1765. {
  1766. if (! stab_write_symbol (info, N_LBRAC, 0, info->pending_lbrac,
  1767. (const char *) NULL))
  1768. return FALSE;
  1769. }
  1770. /* Remember the address and output it later. */
  1771. info->pending_lbrac = addr - info->fnaddr;
  1772. return TRUE;
  1773. }
  1774. /* End a block. */
  1775. static bfd_boolean
  1776. stab_end_block (void *p, bfd_vma addr)
  1777. {
  1778. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1779. if (addr > info->last_text_address)
  1780. info->last_text_address = addr;
  1781. /* If we have postponed an LBRAC, output it now. */
  1782. if (info->pending_lbrac != (bfd_vma) -1)
  1783. {
  1784. if (! stab_write_symbol (info, N_LBRAC, 0, info->pending_lbrac,
  1785. (const char *) NULL))
  1786. return FALSE;
  1787. info->pending_lbrac = (bfd_vma) -1;
  1788. }
  1789. assert (info->nesting > 0);
  1790. --info->nesting;
  1791. /* We ignore the outermost block. */
  1792. if (info->nesting == 0)
  1793. return TRUE;
  1794. return stab_write_symbol (info, N_RBRAC, 0, addr - info->fnaddr,
  1795. (const char *) NULL);
  1796. }
  1797. /* End a function. */
  1798. static bfd_boolean
  1799. stab_end_function (void *p ATTRIBUTE_UNUSED)
  1800. {
  1801. return TRUE;
  1802. }
  1803. /* Output a line number. */
  1804. static bfd_boolean
  1805. stab_lineno (void *p, const char *file, unsigned long lineno, bfd_vma addr)
  1806. {
  1807. struct stab_write_handle *info = (struct stab_write_handle *) p;
  1808. assert (info->lineno_filename != NULL);
  1809. if (addr > info->last_text_address)
  1810. info->last_text_address = addr;
  1811. if (filename_cmp (file, info->lineno_filename) != 0)
  1812. {
  1813. if (! stab_write_symbol (info, N_SOL, 0, addr, file))
  1814. return FALSE;
  1815. info->lineno_filename = file;
  1816. }
  1817. return stab_write_symbol (info, N_SLINE, lineno, addr - info->fnaddr,
  1818. (const char *) NULL);
  1819. }