debug.c 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383
  1. /* debug.c -- Handle generic debugging information.
  2. Copyright (C) 1995-2022 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 implements a generic debugging format. We may eventually
  18. have readers which convert different formats into this generic
  19. format, and writers which write it out. The initial impetus for
  20. this was writing a converter from stabs to HP IEEE-695 debugging
  21. format. */
  22. #include "sysdep.h"
  23. #include <assert.h>
  24. #include "bfd.h"
  25. #include "libiberty.h"
  26. #include "filenames.h"
  27. #include "debug.h"
  28. /* Global information we keep for debugging. A pointer to this
  29. structure is the debugging handle passed to all the routines. */
  30. struct debug_handle
  31. {
  32. /* A linked list of compilation units. */
  33. struct debug_unit *units;
  34. /* The current compilation unit. */
  35. struct debug_unit *current_unit;
  36. /* The current source file. */
  37. struct debug_file *current_file;
  38. /* The current function. */
  39. struct debug_function *current_function;
  40. /* The current block. */
  41. struct debug_block *current_block;
  42. /* The current line number information for the current unit. */
  43. struct debug_lineno *current_lineno;
  44. /* Mark. This is used by debug_write. */
  45. unsigned int mark;
  46. /* A struct/class ID used by debug_write. */
  47. unsigned int class_id;
  48. /* The base for class_id for this call to debug_write. */
  49. unsigned int base_id;
  50. /* The current line number in debug_write. */
  51. struct debug_lineno *current_write_lineno;
  52. unsigned int current_write_lineno_index;
  53. /* A list of classes which have assigned ID's during debug_write.
  54. This is linked through the next_id field of debug_class_type. */
  55. struct debug_class_id *id_list;
  56. /* A list used to avoid recursion during debug_type_samep. */
  57. struct debug_type_compare_list *compare_list;
  58. };
  59. /* Information we keep for a single compilation unit. */
  60. struct debug_unit
  61. {
  62. /* The next compilation unit. */
  63. struct debug_unit *next;
  64. /* A list of files included in this compilation unit. The first
  65. file is always the main one, and that is where the main file name
  66. is stored. */
  67. struct debug_file *files;
  68. /* Line number information for this compilation unit. This is not
  69. stored by function, because assembler code may have line number
  70. information without function information. */
  71. struct debug_lineno *linenos;
  72. };
  73. /* Information kept for a single source file. */
  74. struct debug_file
  75. {
  76. /* The next source file in this compilation unit. */
  77. struct debug_file *next;
  78. /* The name of the source file. */
  79. const char *filename;
  80. /* Global functions, variables, types, etc. */
  81. struct debug_namespace *globals;
  82. };
  83. /* A type. */
  84. struct debug_type_s
  85. {
  86. /* Kind of type. */
  87. enum debug_type_kind kind;
  88. /* Size of type (0 if not known). */
  89. unsigned int size;
  90. /* Type which is a pointer to this type. */
  91. debug_type pointer;
  92. /* Tagged union with additional information about the type. */
  93. union
  94. {
  95. /* DEBUG_KIND_INDIRECT. */
  96. struct debug_indirect_type *kindirect;
  97. /* DEBUG_KIND_INT. */
  98. /* Whether the integer is unsigned. */
  99. bool kint;
  100. /* DEBUG_KIND_STRUCT, DEBUG_KIND_UNION, DEBUG_KIND_CLASS,
  101. DEBUG_KIND_UNION_CLASS. */
  102. struct debug_class_type *kclass;
  103. /* DEBUG_KIND_ENUM. */
  104. struct debug_enum_type *kenum;
  105. /* DEBUG_KIND_POINTER. */
  106. struct debug_type_s *kpointer;
  107. /* DEBUG_KIND_FUNCTION. */
  108. struct debug_function_type *kfunction;
  109. /* DEBUG_KIND_REFERENCE. */
  110. struct debug_type_s *kreference;
  111. /* DEBUG_KIND_RANGE. */
  112. struct debug_range_type *krange;
  113. /* DEBUG_KIND_ARRAY. */
  114. struct debug_array_type *karray;
  115. /* DEBUG_KIND_SET. */
  116. struct debug_set_type *kset;
  117. /* DEBUG_KIND_OFFSET. */
  118. struct debug_offset_type *koffset;
  119. /* DEBUG_KIND_METHOD. */
  120. struct debug_method_type *kmethod;
  121. /* DEBUG_KIND_CONST. */
  122. struct debug_type_s *kconst;
  123. /* DEBUG_KIND_VOLATILE. */
  124. struct debug_type_s *kvolatile;
  125. /* DEBUG_KIND_NAMED, DEBUG_KIND_TAGGED. */
  126. struct debug_named_type *knamed;
  127. } u;
  128. };
  129. /* Information kept for an indirect type. */
  130. struct debug_indirect_type
  131. {
  132. /* Slot where the final type will appear. */
  133. debug_type *slot;
  134. /* Tag. */
  135. const char *tag;
  136. };
  137. /* Information kept for a struct, union, or class. */
  138. struct debug_class_type
  139. {
  140. /* NULL terminated array of fields. */
  141. debug_field *fields;
  142. /* A mark field which indicates whether the struct has already been
  143. printed. */
  144. unsigned int mark;
  145. /* This is used to uniquely identify unnamed structs when printing. */
  146. unsigned int id;
  147. /* The remaining fields are only used for DEBUG_KIND_CLASS and
  148. DEBUG_KIND_UNION_CLASS. */
  149. /* NULL terminated array of base classes. */
  150. debug_baseclass *baseclasses;
  151. /* NULL terminated array of methods. */
  152. debug_method *methods;
  153. /* The type of the class providing the virtual function table for
  154. this class. This may point to the type itself. */
  155. debug_type vptrbase;
  156. };
  157. /* Information kept for an enum. */
  158. struct debug_enum_type
  159. {
  160. /* NULL terminated array of names. */
  161. const char **names;
  162. /* Array of corresponding values. */
  163. bfd_signed_vma *values;
  164. };
  165. /* Information kept for a function. FIXME: We should be able to
  166. record the parameter types. */
  167. struct debug_function_type
  168. {
  169. /* Return type. */
  170. debug_type return_type;
  171. /* NULL terminated array of argument types. */
  172. debug_type *arg_types;
  173. /* Whether the function takes a variable number of arguments. */
  174. bool varargs;
  175. };
  176. /* Information kept for a range. */
  177. struct debug_range_type
  178. {
  179. /* Range base type. */
  180. debug_type type;
  181. /* Lower bound. */
  182. bfd_signed_vma lower;
  183. /* Upper bound. */
  184. bfd_signed_vma upper;
  185. };
  186. /* Information kept for an array. */
  187. struct debug_array_type
  188. {
  189. /* Element type. */
  190. debug_type element_type;
  191. /* Range type. */
  192. debug_type range_type;
  193. /* Lower bound. */
  194. bfd_signed_vma lower;
  195. /* Upper bound. */
  196. bfd_signed_vma upper;
  197. /* Whether this array is really a string. */
  198. bool stringp;
  199. };
  200. /* Information kept for a set. */
  201. struct debug_set_type
  202. {
  203. /* Base type. */
  204. debug_type type;
  205. /* Whether this set is really a bitstring. */
  206. bool bitstringp;
  207. };
  208. /* Information kept for an offset type (a based pointer). */
  209. struct debug_offset_type
  210. {
  211. /* The type the pointer is an offset from. */
  212. debug_type base_type;
  213. /* The type the pointer points to. */
  214. debug_type target_type;
  215. };
  216. /* Information kept for a method type. */
  217. struct debug_method_type
  218. {
  219. /* The return type. */
  220. debug_type return_type;
  221. /* The object type which this method is for. */
  222. debug_type domain_type;
  223. /* A NULL terminated array of argument types. */
  224. debug_type *arg_types;
  225. /* Whether the method takes a variable number of arguments. */
  226. bool varargs;
  227. };
  228. /* Information kept for a named type. */
  229. struct debug_named_type
  230. {
  231. /* Name. */
  232. struct debug_name *name;
  233. /* Real type. */
  234. debug_type type;
  235. };
  236. /* A field in a struct or union. */
  237. struct debug_field_s
  238. {
  239. /* Name of the field. */
  240. const char *name;
  241. /* Type of the field. */
  242. struct debug_type_s *type;
  243. /* Visibility of the field. */
  244. enum debug_visibility visibility;
  245. /* Whether this is a static member. */
  246. bool static_member;
  247. union
  248. {
  249. /* If static_member is false. */
  250. struct
  251. {
  252. /* Bit position of the field in the struct. */
  253. unsigned int bitpos;
  254. /* Size of the field in bits. */
  255. unsigned int bitsize;
  256. } f;
  257. /* If static_member is true. */
  258. struct
  259. {
  260. const char *physname;
  261. } s;
  262. } u;
  263. };
  264. /* A base class for an object. */
  265. struct debug_baseclass_s
  266. {
  267. /* Type of the base class. */
  268. struct debug_type_s *type;
  269. /* Bit position of the base class in the object. */
  270. unsigned int bitpos;
  271. /* Whether the base class is virtual. */
  272. bool is_virtual;
  273. /* Visibility of the base class. */
  274. enum debug_visibility visibility;
  275. };
  276. /* A method of an object. */
  277. struct debug_method_s
  278. {
  279. /* The name of the method. */
  280. const char *name;
  281. /* A NULL terminated array of different types of variants. */
  282. struct debug_method_variant_s **variants;
  283. };
  284. /* The variants of a method function of an object. These indicate
  285. which method to run. */
  286. struct debug_method_variant_s
  287. {
  288. /* The physical name of the function. */
  289. const char *physname;
  290. /* The type of the function. */
  291. struct debug_type_s *type;
  292. /* The visibility of the function. */
  293. enum debug_visibility visibility;
  294. /* Whether the function is const. */
  295. bool constp;
  296. /* Whether the function is volatile. */
  297. bool volatilep;
  298. /* The offset to the function in the virtual function table. */
  299. bfd_vma voffset;
  300. /* If voffset is VOFFSET_STATIC_METHOD, this is a static method. */
  301. #define VOFFSET_STATIC_METHOD ((bfd_vma) -1)
  302. /* Context of a virtual method function. */
  303. struct debug_type_s *context;
  304. };
  305. /* A variable. This is the information we keep for a variable object.
  306. This has no name; a name is associated with a variable in a
  307. debug_name structure. */
  308. struct debug_variable
  309. {
  310. /* Kind of variable. */
  311. enum debug_var_kind kind;
  312. /* Type. */
  313. debug_type type;
  314. /* Value. The interpretation of the value depends upon kind. */
  315. bfd_vma val;
  316. };
  317. /* A function. This has no name; a name is associated with a function
  318. in a debug_name structure. */
  319. struct debug_function
  320. {
  321. /* Return type. */
  322. debug_type return_type;
  323. /* Parameter information. */
  324. struct debug_parameter *parameters;
  325. /* Block information. The first structure on the list is the main
  326. block of the function, and describes function local variables. */
  327. struct debug_block *blocks;
  328. };
  329. /* A function parameter. */
  330. struct debug_parameter
  331. {
  332. /* Next parameter. */
  333. struct debug_parameter *next;
  334. /* Name. */
  335. const char *name;
  336. /* Type. */
  337. debug_type type;
  338. /* Kind. */
  339. enum debug_parm_kind kind;
  340. /* Value (meaning depends upon kind). */
  341. bfd_vma val;
  342. };
  343. /* A typed constant. */
  344. struct debug_typed_constant
  345. {
  346. /* Type. */
  347. debug_type type;
  348. /* Value. FIXME: We may eventually need to support non-integral
  349. values. */
  350. bfd_vma val;
  351. };
  352. /* Information about a block within a function. */
  353. struct debug_block
  354. {
  355. /* Next block with the same parent. */
  356. struct debug_block *next;
  357. /* Parent block. */
  358. struct debug_block *parent;
  359. /* List of child blocks. */
  360. struct debug_block *children;
  361. /* Start address of the block. */
  362. bfd_vma start;
  363. /* End address of the block. */
  364. bfd_vma end;
  365. /* Local variables. */
  366. struct debug_namespace *locals;
  367. };
  368. /* Line number information we keep for a compilation unit. FIXME:
  369. This structure is easy to create, but can be very space
  370. inefficient. */
  371. struct debug_lineno
  372. {
  373. /* More line number information for this block. */
  374. struct debug_lineno *next;
  375. /* Source file. */
  376. struct debug_file *file;
  377. /* Line numbers, terminated by a -1 or the end of the array. */
  378. #define DEBUG_LINENO_COUNT 10
  379. unsigned long linenos[DEBUG_LINENO_COUNT];
  380. /* Addresses for the line numbers. */
  381. bfd_vma addrs[DEBUG_LINENO_COUNT];
  382. };
  383. /* A namespace. This is a mapping from names to objects. FIXME: This
  384. should be implemented as a hash table. */
  385. struct debug_namespace
  386. {
  387. /* List of items in this namespace. */
  388. struct debug_name *list;
  389. /* Pointer to where the next item in this namespace should go. */
  390. struct debug_name **tail;
  391. };
  392. /* Kinds of objects that appear in a namespace. */
  393. enum debug_object_kind
  394. {
  395. /* A type. */
  396. DEBUG_OBJECT_TYPE,
  397. /* A tagged type (really a different sort of namespace). */
  398. DEBUG_OBJECT_TAG,
  399. /* A variable. */
  400. DEBUG_OBJECT_VARIABLE,
  401. /* A function. */
  402. DEBUG_OBJECT_FUNCTION,
  403. /* An integer constant. */
  404. DEBUG_OBJECT_INT_CONSTANT,
  405. /* A floating point constant. */
  406. DEBUG_OBJECT_FLOAT_CONSTANT,
  407. /* A typed constant. */
  408. DEBUG_OBJECT_TYPED_CONSTANT
  409. };
  410. /* Linkage of an object that appears in a namespace. */
  411. enum debug_object_linkage
  412. {
  413. /* Local variable. */
  414. DEBUG_LINKAGE_AUTOMATIC,
  415. /* Static--either file static or function static, depending upon the
  416. namespace is. */
  417. DEBUG_LINKAGE_STATIC,
  418. /* Global. */
  419. DEBUG_LINKAGE_GLOBAL,
  420. /* No linkage. */
  421. DEBUG_LINKAGE_NONE
  422. };
  423. /* A name in a namespace. */
  424. struct debug_name
  425. {
  426. /* Next name in this namespace. */
  427. struct debug_name *next;
  428. /* Name. */
  429. const char *name;
  430. /* Mark. This is used by debug_write. */
  431. unsigned int mark;
  432. /* Kind of object. */
  433. enum debug_object_kind kind;
  434. /* Linkage of object. */
  435. enum debug_object_linkage linkage;
  436. /* Tagged union with additional information about the object. */
  437. union
  438. {
  439. /* DEBUG_OBJECT_TYPE. */
  440. struct debug_type_s *type;
  441. /* DEBUG_OBJECT_TAG. */
  442. struct debug_type_s *tag;
  443. /* DEBUG_OBJECT_VARIABLE. */
  444. struct debug_variable *variable;
  445. /* DEBUG_OBJECT_FUNCTION. */
  446. struct debug_function *function;
  447. /* DEBUG_OBJECT_INT_CONSTANT. */
  448. bfd_vma int_constant;
  449. /* DEBUG_OBJECT_FLOAT_CONSTANT. */
  450. double float_constant;
  451. /* DEBUG_OBJECT_TYPED_CONSTANT. */
  452. struct debug_typed_constant *typed_constant;
  453. } u;
  454. };
  455. /* During debug_write, a linked list of these structures is used to
  456. keep track of ID numbers that have been assigned to classes. */
  457. struct debug_class_id
  458. {
  459. /* Next ID number. */
  460. struct debug_class_id *next;
  461. /* The type with the ID. */
  462. struct debug_type_s *type;
  463. /* The tag; NULL if no tag. */
  464. const char *tag;
  465. };
  466. /* During debug_type_samep, a linked list of these structures is kept
  467. on the stack to avoid infinite recursion. */
  468. struct debug_type_compare_list
  469. {
  470. /* Next type on list. */
  471. struct debug_type_compare_list *next;
  472. /* The types we are comparing. */
  473. struct debug_type_s *t1;
  474. struct debug_type_s *t2;
  475. };
  476. /* During debug_get_real_type, a linked list of these structures is
  477. kept on the stack to avoid infinite recursion. */
  478. struct debug_type_real_list
  479. {
  480. /* Next type on list. */
  481. struct debug_type_real_list *next;
  482. /* The type we are checking. */
  483. struct debug_type_s *t;
  484. };
  485. /* Local functions. */
  486. static void debug_error (const char *);
  487. static struct debug_name *debug_add_to_namespace
  488. (struct debug_handle *, struct debug_namespace **, const char *,
  489. enum debug_object_kind, enum debug_object_linkage);
  490. static struct debug_name *debug_add_to_current_namespace
  491. (struct debug_handle *, const char *, enum debug_object_kind,
  492. enum debug_object_linkage);
  493. static struct debug_type_s *debug_make_type
  494. (struct debug_handle *, enum debug_type_kind, unsigned int);
  495. static struct debug_type_s *debug_get_real_type
  496. (void *, debug_type, struct debug_type_real_list *);
  497. static bool debug_write_name
  498. (struct debug_handle *, const struct debug_write_fns *, void *,
  499. struct debug_name *);
  500. static bool debug_write_type
  501. (struct debug_handle *, const struct debug_write_fns *, void *,
  502. struct debug_type_s *, struct debug_name *);
  503. static bool debug_write_class_type
  504. (struct debug_handle *, const struct debug_write_fns *, void *,
  505. struct debug_type_s *, const char *);
  506. static bool debug_write_function
  507. (struct debug_handle *, const struct debug_write_fns *, void *,
  508. const char *, enum debug_object_linkage, struct debug_function *);
  509. static bool debug_write_block
  510. (struct debug_handle *, const struct debug_write_fns *, void *,
  511. struct debug_block *);
  512. static bool debug_write_linenos
  513. (struct debug_handle *, const struct debug_write_fns *, void *, bfd_vma);
  514. static bool debug_set_class_id
  515. (struct debug_handle *, const char *, struct debug_type_s *);
  516. static bool debug_type_samep
  517. (struct debug_handle *, struct debug_type_s *, struct debug_type_s *);
  518. static bool debug_class_type_samep
  519. (struct debug_handle *, struct debug_type_s *, struct debug_type_s *);
  520. /* Issue an error message. */
  521. static void
  522. debug_error (const char *message)
  523. {
  524. fprintf (stderr, "%s\n", message);
  525. }
  526. /* Add an object to a namespace. */
  527. static struct debug_name *
  528. debug_add_to_namespace (struct debug_handle *info ATTRIBUTE_UNUSED,
  529. struct debug_namespace **nsp, const char *name,
  530. enum debug_object_kind kind,
  531. enum debug_object_linkage linkage)
  532. {
  533. struct debug_name *n;
  534. struct debug_namespace *ns;
  535. n = (struct debug_name *) xmalloc (sizeof *n);
  536. memset (n, 0, sizeof *n);
  537. n->name = name;
  538. n->kind = kind;
  539. n->linkage = linkage;
  540. ns = *nsp;
  541. if (ns == NULL)
  542. {
  543. ns = (struct debug_namespace *) xmalloc (sizeof *ns);
  544. memset (ns, 0, sizeof *ns);
  545. ns->tail = &ns->list;
  546. *nsp = ns;
  547. }
  548. *ns->tail = n;
  549. ns->tail = &n->next;
  550. return n;
  551. }
  552. /* Add an object to the current namespace. */
  553. static struct debug_name *
  554. debug_add_to_current_namespace (struct debug_handle *info, const char *name,
  555. enum debug_object_kind kind,
  556. enum debug_object_linkage linkage)
  557. {
  558. struct debug_namespace **nsp;
  559. if (info->current_unit == NULL
  560. || info->current_file == NULL)
  561. {
  562. debug_error (_("debug_add_to_current_namespace: no current file"));
  563. return NULL;
  564. }
  565. if (info->current_block != NULL)
  566. nsp = &info->current_block->locals;
  567. else
  568. nsp = &info->current_file->globals;
  569. return debug_add_to_namespace (info, nsp, name, kind, linkage);
  570. }
  571. /* Return a handle for debugging information. */
  572. void *
  573. debug_init (void)
  574. {
  575. struct debug_handle *ret;
  576. ret = (struct debug_handle *) xmalloc (sizeof *ret);
  577. memset (ret, 0, sizeof *ret);
  578. return (void *) ret;
  579. }
  580. /* Set the source filename. This implicitly starts a new compilation
  581. unit. */
  582. bool
  583. debug_set_filename (void *handle, const char *name)
  584. {
  585. struct debug_handle *info = (struct debug_handle *) handle;
  586. struct debug_file *nfile;
  587. struct debug_unit *nunit;
  588. if (name == NULL)
  589. name = "";
  590. nfile = (struct debug_file *) xmalloc (sizeof *nfile);
  591. memset (nfile, 0, sizeof *nfile);
  592. nfile->filename = name;
  593. nunit = (struct debug_unit *) xmalloc (sizeof *nunit);
  594. memset (nunit, 0, sizeof *nunit);
  595. nunit->files = nfile;
  596. info->current_file = nfile;
  597. if (info->current_unit != NULL)
  598. info->current_unit->next = nunit;
  599. else
  600. {
  601. assert (info->units == NULL);
  602. info->units = nunit;
  603. }
  604. info->current_unit = nunit;
  605. info->current_function = NULL;
  606. info->current_block = NULL;
  607. info->current_lineno = NULL;
  608. return true;
  609. }
  610. /* Change source files to the given file name. This is used for
  611. include files in a single compilation unit. */
  612. bool
  613. debug_start_source (void *handle, const char *name)
  614. {
  615. struct debug_handle *info = (struct debug_handle *) handle;
  616. struct debug_file *f, **pf;
  617. if (name == NULL)
  618. name = "";
  619. if (info->current_unit == NULL)
  620. {
  621. debug_error (_("debug_start_source: no debug_set_filename call"));
  622. return false;
  623. }
  624. for (f = info->current_unit->files; f != NULL; f = f->next)
  625. {
  626. if (filename_cmp (f->filename, name) == 0)
  627. {
  628. info->current_file = f;
  629. return true;
  630. }
  631. }
  632. f = (struct debug_file *) xmalloc (sizeof *f);
  633. memset (f, 0, sizeof *f);
  634. f->filename = name;
  635. for (pf = &info->current_file->next;
  636. *pf != NULL;
  637. pf = &(*pf)->next)
  638. ;
  639. *pf = f;
  640. info->current_file = f;
  641. return true;
  642. }
  643. /* Record a function definition. This implicitly starts a function
  644. block. The debug_type argument is the type of the return value.
  645. The boolean indicates whether the function is globally visible.
  646. The bfd_vma is the address of the start of the function. Currently
  647. the parameter types are specified by calls to
  648. debug_record_parameter. FIXME: There is no way to specify nested
  649. functions. */
  650. bool
  651. debug_record_function (void *handle, const char *name,
  652. debug_type return_type, bool global,
  653. bfd_vma addr)
  654. {
  655. struct debug_handle *info = (struct debug_handle *) handle;
  656. struct debug_function *f;
  657. struct debug_block *b;
  658. struct debug_name *n;
  659. if (name == NULL)
  660. name = "";
  661. if (return_type == NULL)
  662. return false;
  663. if (info->current_unit == NULL)
  664. {
  665. debug_error (_("debug_record_function: no debug_set_filename call"));
  666. return false;
  667. }
  668. f = (struct debug_function *) xmalloc (sizeof *f);
  669. memset (f, 0, sizeof *f);
  670. f->return_type = return_type;
  671. b = (struct debug_block *) xmalloc (sizeof *b);
  672. memset (b, 0, sizeof *b);
  673. b->start = addr;
  674. b->end = (bfd_vma) -1;
  675. f->blocks = b;
  676. info->current_function = f;
  677. info->current_block = b;
  678. /* FIXME: If we could handle nested functions, this would be the
  679. place: we would want to use a different namespace. */
  680. n = debug_add_to_namespace (info,
  681. &info->current_file->globals,
  682. name,
  683. DEBUG_OBJECT_FUNCTION,
  684. (global
  685. ? DEBUG_LINKAGE_GLOBAL
  686. : DEBUG_LINKAGE_STATIC));
  687. if (n == NULL)
  688. return false;
  689. n->u.function = f;
  690. return true;
  691. }
  692. /* Record a parameter for the current function. */
  693. bool
  694. debug_record_parameter (void *handle, const char *name, debug_type type,
  695. enum debug_parm_kind kind, bfd_vma val)
  696. {
  697. struct debug_handle *info = (struct debug_handle *) handle;
  698. struct debug_parameter *p, **pp;
  699. if (name == NULL || type == NULL)
  700. return false;
  701. if (info->current_unit == NULL
  702. || info->current_function == NULL)
  703. {
  704. debug_error (_("debug_record_parameter: no current function"));
  705. return false;
  706. }
  707. p = (struct debug_parameter *) xmalloc (sizeof *p);
  708. memset (p, 0, sizeof *p);
  709. p->name = name;
  710. p->type = type;
  711. p->kind = kind;
  712. p->val = val;
  713. for (pp = &info->current_function->parameters;
  714. *pp != NULL;
  715. pp = &(*pp)->next)
  716. ;
  717. *pp = p;
  718. return true;
  719. }
  720. /* End a function. FIXME: This should handle function nesting. */
  721. bool
  722. debug_end_function (void *handle, bfd_vma addr)
  723. {
  724. struct debug_handle *info = (struct debug_handle *) handle;
  725. if (info->current_unit == NULL
  726. || info->current_block == NULL
  727. || info->current_function == NULL)
  728. {
  729. debug_error (_("debug_end_function: no current function"));
  730. return false;
  731. }
  732. if (info->current_block->parent != NULL)
  733. {
  734. debug_error (_("debug_end_function: some blocks were not closed"));
  735. return false;
  736. }
  737. info->current_block->end = addr;
  738. info->current_function = NULL;
  739. info->current_block = NULL;
  740. return true;
  741. }
  742. /* Start a block in a function. All local information will be
  743. recorded in this block, until the matching call to debug_end_block.
  744. debug_start_block and debug_end_block may be nested. The bfd_vma
  745. argument is the address at which this block starts. */
  746. bool
  747. debug_start_block (void *handle, bfd_vma addr)
  748. {
  749. struct debug_handle *info = (struct debug_handle *) handle;
  750. struct debug_block *b, **pb;
  751. /* We must always have a current block: debug_record_function sets
  752. one up. */
  753. if (info->current_unit == NULL
  754. || info->current_block == NULL)
  755. {
  756. debug_error (_("debug_start_block: no current block"));
  757. return false;
  758. }
  759. b = (struct debug_block *) xmalloc (sizeof *b);
  760. memset (b, 0, sizeof *b);
  761. b->parent = info->current_block;
  762. b->start = addr;
  763. b->end = (bfd_vma) -1;
  764. /* This new block is a child of the current block. */
  765. for (pb = &info->current_block->children;
  766. *pb != NULL;
  767. pb = &(*pb)->next)
  768. ;
  769. *pb = b;
  770. info->current_block = b;
  771. return true;
  772. }
  773. /* Finish a block in a function. This matches the call to
  774. debug_start_block. The argument is the address at which this block
  775. ends. */
  776. bool
  777. debug_end_block (void *handle, bfd_vma addr)
  778. {
  779. struct debug_handle *info = (struct debug_handle *) handle;
  780. struct debug_block *parent;
  781. if (info->current_unit == NULL
  782. || info->current_block == NULL)
  783. {
  784. debug_error (_("debug_end_block: no current block"));
  785. return false;
  786. }
  787. parent = info->current_block->parent;
  788. if (parent == NULL)
  789. {
  790. debug_error (_("debug_end_block: attempt to close top level block"));
  791. return false;
  792. }
  793. info->current_block->end = addr;
  794. info->current_block = parent;
  795. return true;
  796. }
  797. /* Associate a line number in the current source file and function
  798. with a given address. */
  799. bool
  800. debug_record_line (void *handle, unsigned long lineno, bfd_vma addr)
  801. {
  802. struct debug_handle *info = (struct debug_handle *) handle;
  803. struct debug_lineno *l;
  804. unsigned int i;
  805. if (info->current_unit == NULL)
  806. {
  807. debug_error (_("debug_record_line: no current unit"));
  808. return false;
  809. }
  810. l = info->current_lineno;
  811. if (l != NULL && l->file == info->current_file)
  812. {
  813. for (i = 0; i < DEBUG_LINENO_COUNT; i++)
  814. {
  815. if (l->linenos[i] == (unsigned long) -1)
  816. {
  817. l->linenos[i] = lineno;
  818. l->addrs[i] = addr;
  819. return true;
  820. }
  821. }
  822. }
  823. /* If we get here, then either 1) there is no current_lineno
  824. structure, which means this is the first line number in this
  825. compilation unit, 2) the current_lineno structure is for a
  826. different file, or 3) the current_lineno structure is full.
  827. Regardless, we want to allocate a new debug_lineno structure, put
  828. it in the right place, and make it the new current_lineno
  829. structure. */
  830. l = (struct debug_lineno *) xmalloc (sizeof *l);
  831. memset (l, 0, sizeof *l);
  832. l->file = info->current_file;
  833. l->linenos[0] = lineno;
  834. l->addrs[0] = addr;
  835. for (i = 1; i < DEBUG_LINENO_COUNT; i++)
  836. l->linenos[i] = (unsigned long) -1;
  837. if (info->current_lineno != NULL)
  838. info->current_lineno->next = l;
  839. else
  840. info->current_unit->linenos = l;
  841. info->current_lineno = l;
  842. return true;
  843. }
  844. /* Start a named common block. This is a block of variables that may
  845. move in memory. */
  846. bool
  847. debug_start_common_block (void *handle ATTRIBUTE_UNUSED,
  848. const char *name ATTRIBUTE_UNUSED)
  849. {
  850. /* FIXME */
  851. debug_error (_("debug_start_common_block: not implemented"));
  852. return false;
  853. }
  854. /* End a named common block. */
  855. bool
  856. debug_end_common_block (void *handle ATTRIBUTE_UNUSED,
  857. const char *name ATTRIBUTE_UNUSED)
  858. {
  859. /* FIXME */
  860. debug_error (_("debug_end_common_block: not implemented"));
  861. return false;
  862. }
  863. /* Record a named integer constant. */
  864. bool
  865. debug_record_int_const (void *handle, const char *name, bfd_vma val)
  866. {
  867. struct debug_handle *info = (struct debug_handle *) handle;
  868. struct debug_name *n;
  869. if (name == NULL)
  870. return false;
  871. n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_INT_CONSTANT,
  872. DEBUG_LINKAGE_NONE);
  873. if (n == NULL)
  874. return false;
  875. n->u.int_constant = val;
  876. return true;
  877. }
  878. /* Record a named floating point constant. */
  879. bool
  880. debug_record_float_const (void *handle, const char *name, double val)
  881. {
  882. struct debug_handle *info = (struct debug_handle *) handle;
  883. struct debug_name *n;
  884. if (name == NULL)
  885. return false;
  886. n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_FLOAT_CONSTANT,
  887. DEBUG_LINKAGE_NONE);
  888. if (n == NULL)
  889. return false;
  890. n->u.float_constant = val;
  891. return true;
  892. }
  893. /* Record a typed constant with an integral value. */
  894. bool
  895. debug_record_typed_const (void *handle, const char *name, debug_type type,
  896. bfd_vma val)
  897. {
  898. struct debug_handle *info = (struct debug_handle *) handle;
  899. struct debug_name *n;
  900. struct debug_typed_constant *tc;
  901. if (name == NULL || type == NULL)
  902. return false;
  903. n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_TYPED_CONSTANT,
  904. DEBUG_LINKAGE_NONE);
  905. if (n == NULL)
  906. return false;
  907. tc = (struct debug_typed_constant *) xmalloc (sizeof *tc);
  908. memset (tc, 0, sizeof *tc);
  909. tc->type = type;
  910. tc->val = val;
  911. n->u.typed_constant = tc;
  912. return true;
  913. }
  914. /* Record a label. */
  915. bool
  916. debug_record_label (void *handle ATTRIBUTE_UNUSED,
  917. const char *name ATTRIBUTE_UNUSED,
  918. debug_type type ATTRIBUTE_UNUSED,
  919. bfd_vma addr ATTRIBUTE_UNUSED)
  920. {
  921. /* FIXME. */
  922. debug_error (_("debug_record_label: not implemented"));
  923. return false;
  924. }
  925. /* Record a variable. */
  926. bool
  927. debug_record_variable (void *handle, const char *name, debug_type type,
  928. enum debug_var_kind kind, bfd_vma val)
  929. {
  930. struct debug_handle *info = (struct debug_handle *) handle;
  931. struct debug_namespace **nsp;
  932. enum debug_object_linkage linkage;
  933. struct debug_name *n;
  934. struct debug_variable *v;
  935. if (name == NULL || type == NULL)
  936. return false;
  937. if (info->current_unit == NULL
  938. || info->current_file == NULL)
  939. {
  940. debug_error (_("debug_record_variable: no current file"));
  941. return false;
  942. }
  943. if (kind == DEBUG_GLOBAL || kind == DEBUG_STATIC)
  944. {
  945. nsp = &info->current_file->globals;
  946. if (kind == DEBUG_GLOBAL)
  947. linkage = DEBUG_LINKAGE_GLOBAL;
  948. else
  949. linkage = DEBUG_LINKAGE_STATIC;
  950. }
  951. else
  952. {
  953. if (info->current_block == NULL)
  954. nsp = &info->current_file->globals;
  955. else
  956. nsp = &info->current_block->locals;
  957. linkage = DEBUG_LINKAGE_AUTOMATIC;
  958. }
  959. n = debug_add_to_namespace (info, nsp, name, DEBUG_OBJECT_VARIABLE, linkage);
  960. if (n == NULL)
  961. return false;
  962. v = (struct debug_variable *) xmalloc (sizeof *v);
  963. memset (v, 0, sizeof *v);
  964. v->kind = kind;
  965. v->type = type;
  966. v->val = val;
  967. n->u.variable = v;
  968. return true;
  969. }
  970. /* Make a type with a given kind and size. */
  971. static struct debug_type_s *
  972. debug_make_type (struct debug_handle *info ATTRIBUTE_UNUSED,
  973. enum debug_type_kind kind, unsigned int size)
  974. {
  975. struct debug_type_s *t;
  976. t = (struct debug_type_s *) xmalloc (sizeof *t);
  977. memset (t, 0, sizeof *t);
  978. t->kind = kind;
  979. t->size = size;
  980. return t;
  981. }
  982. /* Make an indirect type which may be used as a placeholder for a type
  983. which is referenced before it is defined. */
  984. debug_type
  985. debug_make_indirect_type (void *handle, debug_type *slot, const char *tag)
  986. {
  987. struct debug_handle *info = (struct debug_handle *) handle;
  988. struct debug_type_s *t;
  989. struct debug_indirect_type *i;
  990. t = debug_make_type (info, DEBUG_KIND_INDIRECT, 0);
  991. if (t == NULL)
  992. return DEBUG_TYPE_NULL;
  993. i = (struct debug_indirect_type *) xmalloc (sizeof *i);
  994. memset (i, 0, sizeof *i);
  995. i->slot = slot;
  996. i->tag = tag;
  997. t->u.kindirect = i;
  998. return t;
  999. }
  1000. /* Make a void type. There is only one of these. */
  1001. debug_type
  1002. debug_make_void_type (void *handle)
  1003. {
  1004. struct debug_handle *info = (struct debug_handle *) handle;
  1005. return debug_make_type (info, DEBUG_KIND_VOID, 0);
  1006. }
  1007. /* Make an integer type of a given size. The boolean argument is true
  1008. if the integer is unsigned. */
  1009. debug_type
  1010. debug_make_int_type (void *handle, unsigned int size, bool unsignedp)
  1011. {
  1012. struct debug_handle *info = (struct debug_handle *) handle;
  1013. struct debug_type_s *t;
  1014. t = debug_make_type (info, DEBUG_KIND_INT, size);
  1015. if (t == NULL)
  1016. return DEBUG_TYPE_NULL;
  1017. t->u.kint = unsignedp;
  1018. return t;
  1019. }
  1020. /* Make a floating point type of a given size. FIXME: On some
  1021. platforms, like an Alpha, you probably need to be able to specify
  1022. the format. */
  1023. debug_type
  1024. debug_make_float_type (void *handle, unsigned int size)
  1025. {
  1026. struct debug_handle *info = (struct debug_handle *) handle;
  1027. return debug_make_type (info, DEBUG_KIND_FLOAT, size);
  1028. }
  1029. /* Make a boolean type of a given size. */
  1030. debug_type
  1031. debug_make_bool_type (void *handle, unsigned int size)
  1032. {
  1033. struct debug_handle *info = (struct debug_handle *) handle;
  1034. return debug_make_type (info, DEBUG_KIND_BOOL, size);
  1035. }
  1036. /* Make a complex type of a given size. */
  1037. debug_type
  1038. debug_make_complex_type (void *handle, unsigned int size)
  1039. {
  1040. struct debug_handle *info = (struct debug_handle *) handle;
  1041. return debug_make_type (info, DEBUG_KIND_COMPLEX, size);
  1042. }
  1043. /* Make a structure type. The second argument is true for a struct,
  1044. false for a union. The third argument is the size of the struct.
  1045. The fourth argument is a NULL terminated array of fields. */
  1046. debug_type
  1047. debug_make_struct_type (void *handle, bool structp, bfd_vma size,
  1048. debug_field *fields)
  1049. {
  1050. struct debug_handle *info = (struct debug_handle *) handle;
  1051. struct debug_type_s *t;
  1052. struct debug_class_type *c;
  1053. t = debug_make_type (info,
  1054. structp ? DEBUG_KIND_STRUCT : DEBUG_KIND_UNION,
  1055. size);
  1056. if (t == NULL)
  1057. return DEBUG_TYPE_NULL;
  1058. c = (struct debug_class_type *) xmalloc (sizeof *c);
  1059. memset (c, 0, sizeof *c);
  1060. c->fields = fields;
  1061. t->u.kclass = c;
  1062. return t;
  1063. }
  1064. /* Make an object type. The first three arguments after the handle
  1065. are the same as for debug_make_struct_type. The next arguments are
  1066. a NULL terminated array of base classes, a NULL terminated array of
  1067. methods, the type of the object holding the virtual function table
  1068. if it is not this object, and a boolean which is true if this
  1069. object has its own virtual function table. */
  1070. debug_type
  1071. debug_make_object_type (void *handle, bool structp, bfd_vma size,
  1072. debug_field *fields, debug_baseclass *baseclasses,
  1073. debug_method *methods, debug_type vptrbase,
  1074. bool ownvptr)
  1075. {
  1076. struct debug_handle *info = (struct debug_handle *) handle;
  1077. struct debug_type_s *t;
  1078. struct debug_class_type *c;
  1079. t = debug_make_type (info,
  1080. structp ? DEBUG_KIND_CLASS : DEBUG_KIND_UNION_CLASS,
  1081. size);
  1082. if (t == NULL)
  1083. return DEBUG_TYPE_NULL;
  1084. c = (struct debug_class_type *) xmalloc (sizeof *c);
  1085. memset (c, 0, sizeof *c);
  1086. c->fields = fields;
  1087. c->baseclasses = baseclasses;
  1088. c->methods = methods;
  1089. if (ownvptr)
  1090. c->vptrbase = t;
  1091. else
  1092. c->vptrbase = vptrbase;
  1093. t->u.kclass = c;
  1094. return t;
  1095. }
  1096. /* Make an enumeration type. The arguments are a null terminated
  1097. array of strings, and an array of corresponding values. */
  1098. debug_type
  1099. debug_make_enum_type (void *handle, const char **names,
  1100. bfd_signed_vma *values)
  1101. {
  1102. struct debug_handle *info = (struct debug_handle *) handle;
  1103. struct debug_type_s *t;
  1104. struct debug_enum_type *e;
  1105. t = debug_make_type (info, DEBUG_KIND_ENUM, 0);
  1106. if (t == NULL)
  1107. return DEBUG_TYPE_NULL;
  1108. e = (struct debug_enum_type *) xmalloc (sizeof *e);
  1109. memset (e, 0, sizeof *e);
  1110. e->names = names;
  1111. e->values = values;
  1112. t->u.kenum = e;
  1113. return t;
  1114. }
  1115. /* Make a pointer to a given type. */
  1116. debug_type
  1117. debug_make_pointer_type (void *handle, debug_type type)
  1118. {
  1119. struct debug_handle *info = (struct debug_handle *) handle;
  1120. struct debug_type_s *t;
  1121. if (type == NULL)
  1122. return DEBUG_TYPE_NULL;
  1123. if (type->pointer != DEBUG_TYPE_NULL)
  1124. return type->pointer;
  1125. t = debug_make_type (info, DEBUG_KIND_POINTER, 0);
  1126. if (t == NULL)
  1127. return DEBUG_TYPE_NULL;
  1128. t->u.kpointer = type;
  1129. type->pointer = t;
  1130. return t;
  1131. }
  1132. /* Make a function returning a given type. FIXME: We should be able
  1133. to record the parameter types. */
  1134. debug_type
  1135. debug_make_function_type (void *handle, debug_type type,
  1136. debug_type *arg_types, bool varargs)
  1137. {
  1138. struct debug_handle *info = (struct debug_handle *) handle;
  1139. struct debug_type_s *t;
  1140. struct debug_function_type *f;
  1141. if (type == NULL)
  1142. return DEBUG_TYPE_NULL;
  1143. t = debug_make_type (info, DEBUG_KIND_FUNCTION, 0);
  1144. if (t == NULL)
  1145. return DEBUG_TYPE_NULL;
  1146. f = (struct debug_function_type *) xmalloc (sizeof *f);
  1147. memset (f, 0, sizeof *f);
  1148. f->return_type = type;
  1149. f->arg_types = arg_types;
  1150. f->varargs = varargs;
  1151. t->u.kfunction = f;
  1152. return t;
  1153. }
  1154. /* Make a reference to a given type. */
  1155. debug_type
  1156. debug_make_reference_type (void *handle, debug_type type)
  1157. {
  1158. struct debug_handle *info = (struct debug_handle *) handle;
  1159. struct debug_type_s *t;
  1160. if (type == NULL)
  1161. return DEBUG_TYPE_NULL;
  1162. t = debug_make_type (info, DEBUG_KIND_REFERENCE, 0);
  1163. if (t == NULL)
  1164. return DEBUG_TYPE_NULL;
  1165. t->u.kreference = type;
  1166. return t;
  1167. }
  1168. /* Make a range of a given type from a lower to an upper bound. */
  1169. debug_type
  1170. debug_make_range_type (void *handle, debug_type type, bfd_signed_vma lower,
  1171. bfd_signed_vma upper)
  1172. {
  1173. struct debug_handle *info = (struct debug_handle *) handle;
  1174. struct debug_type_s *t;
  1175. struct debug_range_type *r;
  1176. if (type == NULL)
  1177. return DEBUG_TYPE_NULL;
  1178. t = debug_make_type (info, DEBUG_KIND_RANGE, 0);
  1179. if (t == NULL)
  1180. return DEBUG_TYPE_NULL;
  1181. r = (struct debug_range_type *) xmalloc (sizeof *r);
  1182. memset (r, 0, sizeof *r);
  1183. r->type = type;
  1184. r->lower = lower;
  1185. r->upper = upper;
  1186. t->u.krange = r;
  1187. return t;
  1188. }
  1189. /* Make an array type. The second argument is the type of an element
  1190. of the array. The third argument is the type of a range of the
  1191. array. The fourth and fifth argument are the lower and upper
  1192. bounds, respectively. The sixth argument is true if this array is
  1193. actually a string, as in C. */
  1194. debug_type
  1195. debug_make_array_type (void *handle, debug_type element_type,
  1196. debug_type range_type, bfd_signed_vma lower,
  1197. bfd_signed_vma upper, bool stringp)
  1198. {
  1199. struct debug_handle *info = (struct debug_handle *) handle;
  1200. struct debug_type_s *t;
  1201. struct debug_array_type *a;
  1202. if (element_type == NULL || range_type == NULL)
  1203. return DEBUG_TYPE_NULL;
  1204. t = debug_make_type (info, DEBUG_KIND_ARRAY, 0);
  1205. if (t == NULL)
  1206. return DEBUG_TYPE_NULL;
  1207. a = (struct debug_array_type *) xmalloc (sizeof *a);
  1208. memset (a, 0, sizeof *a);
  1209. a->element_type = element_type;
  1210. a->range_type = range_type;
  1211. a->lower = lower;
  1212. a->upper = upper;
  1213. a->stringp = stringp;
  1214. t->u.karray = a;
  1215. return t;
  1216. }
  1217. /* Make a set of a given type. For example, a Pascal set type. The
  1218. boolean argument is true if this set is actually a bitstring, as in
  1219. CHILL. */
  1220. debug_type
  1221. debug_make_set_type (void *handle, debug_type type, bool bitstringp)
  1222. {
  1223. struct debug_handle *info = (struct debug_handle *) handle;
  1224. struct debug_type_s *t;
  1225. struct debug_set_type *s;
  1226. if (type == NULL)
  1227. return DEBUG_TYPE_NULL;
  1228. t = debug_make_type (info, DEBUG_KIND_SET, 0);
  1229. if (t == NULL)
  1230. return DEBUG_TYPE_NULL;
  1231. s = (struct debug_set_type *) xmalloc (sizeof *s);
  1232. memset (s, 0, sizeof *s);
  1233. s->type = type;
  1234. s->bitstringp = bitstringp;
  1235. t->u.kset = s;
  1236. return t;
  1237. }
  1238. /* Make a type for a pointer which is relative to an object. The
  1239. second argument is the type of the object to which the pointer is
  1240. relative. The third argument is the type that the pointer points
  1241. to. */
  1242. debug_type
  1243. debug_make_offset_type (void *handle, debug_type base_type,
  1244. debug_type target_type)
  1245. {
  1246. struct debug_handle *info = (struct debug_handle *) handle;
  1247. struct debug_type_s *t;
  1248. struct debug_offset_type *o;
  1249. if (base_type == NULL || target_type == NULL)
  1250. return DEBUG_TYPE_NULL;
  1251. t = debug_make_type (info, DEBUG_KIND_OFFSET, 0);
  1252. if (t == NULL)
  1253. return DEBUG_TYPE_NULL;
  1254. o = (struct debug_offset_type *) xmalloc (sizeof *o);
  1255. memset (o, 0, sizeof *o);
  1256. o->base_type = base_type;
  1257. o->target_type = target_type;
  1258. t->u.koffset = o;
  1259. return t;
  1260. }
  1261. /* Make a type for a method function. The second argument is the
  1262. return type, the third argument is the domain, and the fourth
  1263. argument is a NULL terminated array of argument types. */
  1264. debug_type
  1265. debug_make_method_type (void *handle, debug_type return_type,
  1266. debug_type domain_type, debug_type *arg_types,
  1267. bool varargs)
  1268. {
  1269. struct debug_handle *info = (struct debug_handle *) handle;
  1270. struct debug_type_s *t;
  1271. struct debug_method_type *m;
  1272. if (return_type == NULL)
  1273. return DEBUG_TYPE_NULL;
  1274. t = debug_make_type (info, DEBUG_KIND_METHOD, 0);
  1275. if (t == NULL)
  1276. return DEBUG_TYPE_NULL;
  1277. m = (struct debug_method_type *) xmalloc (sizeof *m);
  1278. memset (m, 0, sizeof *m);
  1279. m->return_type = return_type;
  1280. m->domain_type = domain_type;
  1281. m->arg_types = arg_types;
  1282. m->varargs = varargs;
  1283. t->u.kmethod = m;
  1284. return t;
  1285. }
  1286. /* Make a const qualified version of a given type. */
  1287. debug_type
  1288. debug_make_const_type (void *handle, debug_type type)
  1289. {
  1290. struct debug_handle *info = (struct debug_handle *) handle;
  1291. struct debug_type_s *t;
  1292. if (type == NULL)
  1293. return DEBUG_TYPE_NULL;
  1294. t = debug_make_type (info, DEBUG_KIND_CONST, 0);
  1295. if (t == NULL)
  1296. return DEBUG_TYPE_NULL;
  1297. t->u.kconst = type;
  1298. return t;
  1299. }
  1300. /* Make a volatile qualified version of a given type. */
  1301. debug_type
  1302. debug_make_volatile_type (void *handle, debug_type type)
  1303. {
  1304. struct debug_handle *info = (struct debug_handle *) handle;
  1305. struct debug_type_s *t;
  1306. if (type == NULL)
  1307. return DEBUG_TYPE_NULL;
  1308. t = debug_make_type (info, DEBUG_KIND_VOLATILE, 0);
  1309. if (t == NULL)
  1310. return DEBUG_TYPE_NULL;
  1311. t->u.kvolatile = type;
  1312. return t;
  1313. }
  1314. /* Make an undefined tagged type. For example, a struct which has
  1315. been mentioned, but not defined. */
  1316. debug_type
  1317. debug_make_undefined_tagged_type (void *handle, const char *name,
  1318. enum debug_type_kind kind)
  1319. {
  1320. struct debug_handle *info = (struct debug_handle *) handle;
  1321. struct debug_type_s *t;
  1322. if (name == NULL)
  1323. return DEBUG_TYPE_NULL;
  1324. switch (kind)
  1325. {
  1326. case DEBUG_KIND_STRUCT:
  1327. case DEBUG_KIND_UNION:
  1328. case DEBUG_KIND_CLASS:
  1329. case DEBUG_KIND_UNION_CLASS:
  1330. case DEBUG_KIND_ENUM:
  1331. break;
  1332. default:
  1333. debug_error (_("debug_make_undefined_type: unsupported kind"));
  1334. return DEBUG_TYPE_NULL;
  1335. }
  1336. t = debug_make_type (info, kind, 0);
  1337. if (t == NULL)
  1338. return DEBUG_TYPE_NULL;
  1339. return debug_tag_type (handle, name, t);
  1340. }
  1341. /* Make a base class for an object. The second argument is the base
  1342. class type. The third argument is the bit position of this base
  1343. class in the object (always 0 unless doing multiple inheritance).
  1344. The fourth argument is whether this is a virtual class. The fifth
  1345. argument is the visibility of the base class. */
  1346. debug_baseclass
  1347. debug_make_baseclass (void *handle ATTRIBUTE_UNUSED, debug_type type,
  1348. bfd_vma bitpos, bool is_virtual,
  1349. enum debug_visibility visibility)
  1350. {
  1351. struct debug_baseclass_s *b;
  1352. b = (struct debug_baseclass_s *) xmalloc (sizeof *b);
  1353. memset (b, 0, sizeof *b);
  1354. b->type = type;
  1355. b->bitpos = bitpos;
  1356. b->is_virtual = is_virtual;
  1357. b->visibility = visibility;
  1358. return b;
  1359. }
  1360. /* Make a field for a struct. The second argument is the name. The
  1361. third argument is the type of the field. The fourth argument is
  1362. the bit position of the field. The fifth argument is the size of
  1363. the field (it may be zero). The sixth argument is the visibility
  1364. of the field. */
  1365. debug_field
  1366. debug_make_field (void *handle ATTRIBUTE_UNUSED, const char *name,
  1367. debug_type type, bfd_vma bitpos, bfd_vma bitsize,
  1368. enum debug_visibility visibility)
  1369. {
  1370. struct debug_field_s *f;
  1371. f = (struct debug_field_s *) xmalloc (sizeof *f);
  1372. memset (f, 0, sizeof *f);
  1373. f->name = name;
  1374. f->type = type;
  1375. f->static_member = false;
  1376. f->u.f.bitpos = bitpos;
  1377. f->u.f.bitsize = bitsize;
  1378. f->visibility = visibility;
  1379. return f;
  1380. }
  1381. /* Make a static member of an object. The second argument is the
  1382. name. The third argument is the type of the member. The fourth
  1383. argument is the physical name of the member (i.e., the name as a
  1384. global variable). The fifth argument is the visibility of the
  1385. member. */
  1386. debug_field
  1387. debug_make_static_member (void *handle ATTRIBUTE_UNUSED, const char *name,
  1388. debug_type type, const char *physname,
  1389. enum debug_visibility visibility)
  1390. {
  1391. struct debug_field_s *f;
  1392. f = (struct debug_field_s *) xmalloc (sizeof *f);
  1393. memset (f, 0, sizeof *f);
  1394. f->name = name;
  1395. f->type = type;
  1396. f->static_member = true;
  1397. f->u.s.physname = physname;
  1398. f->visibility = visibility;
  1399. return f;
  1400. }
  1401. /* Make a method. The second argument is the name, and the third
  1402. argument is a NULL terminated array of method variants. */
  1403. debug_method
  1404. debug_make_method (void *handle ATTRIBUTE_UNUSED, const char *name,
  1405. debug_method_variant *variants)
  1406. {
  1407. struct debug_method_s *m;
  1408. m = (struct debug_method_s *) xmalloc (sizeof *m);
  1409. memset (m, 0, sizeof *m);
  1410. m->name = name;
  1411. m->variants = variants;
  1412. return m;
  1413. }
  1414. /* Make a method argument. The second argument is the real name of
  1415. the function. The third argument is the type of the function. The
  1416. fourth argument is the visibility. The fifth argument is whether
  1417. this is a const function. The sixth argument is whether this is a
  1418. volatile function. The seventh argument is the offset in the
  1419. virtual function table, if any. The eighth argument is the virtual
  1420. function context. FIXME: Are the const and volatile arguments
  1421. necessary? Could we just use debug_make_const_type? */
  1422. debug_method_variant
  1423. debug_make_method_variant (void *handle ATTRIBUTE_UNUSED,
  1424. const char *physname, debug_type type,
  1425. enum debug_visibility visibility,
  1426. bool constp, bool volatilep,
  1427. bfd_vma voffset, debug_type context)
  1428. {
  1429. struct debug_method_variant_s *m;
  1430. m = (struct debug_method_variant_s *) xmalloc (sizeof *m);
  1431. memset (m, 0, sizeof *m);
  1432. m->physname = physname;
  1433. m->type = type;
  1434. m->visibility = visibility;
  1435. m->constp = constp;
  1436. m->volatilep = volatilep;
  1437. m->voffset = voffset;
  1438. m->context = context;
  1439. return m;
  1440. }
  1441. /* Make a static method argument. The arguments are the same as for
  1442. debug_make_method_variant, except that the last two are omitted
  1443. since a static method can not also be virtual. */
  1444. debug_method_variant
  1445. debug_make_static_method_variant (void *handle ATTRIBUTE_UNUSED,
  1446. const char *physname, debug_type type,
  1447. enum debug_visibility visibility,
  1448. bool constp, bool volatilep)
  1449. {
  1450. struct debug_method_variant_s *m;
  1451. m = (struct debug_method_variant_s *) xmalloc (sizeof *m);
  1452. memset (m, 0, sizeof *m);
  1453. m->physname = physname;
  1454. m->type = type;
  1455. m->visibility = visibility;
  1456. m->constp = constp;
  1457. m->volatilep = volatilep;
  1458. m->voffset = VOFFSET_STATIC_METHOD;
  1459. return m;
  1460. }
  1461. /* Name a type. */
  1462. debug_type
  1463. debug_name_type (void *handle, const char *name, debug_type type)
  1464. {
  1465. struct debug_handle *info = (struct debug_handle *) handle;
  1466. struct debug_type_s *t;
  1467. struct debug_named_type *n;
  1468. struct debug_name *nm;
  1469. if (name == NULL || type == NULL)
  1470. return DEBUG_TYPE_NULL;
  1471. if (info->current_unit == NULL
  1472. || info->current_file == NULL)
  1473. {
  1474. debug_error (_("debug_name_type: no current file"));
  1475. return DEBUG_TYPE_NULL;
  1476. }
  1477. t = debug_make_type (info, DEBUG_KIND_NAMED, 0);
  1478. if (t == NULL)
  1479. return DEBUG_TYPE_NULL;
  1480. n = (struct debug_named_type *) xmalloc (sizeof *n);
  1481. memset (n, 0, sizeof *n);
  1482. n->type = type;
  1483. t->u.knamed = n;
  1484. /* We always add the name to the global namespace. This is probably
  1485. wrong in some cases, but it seems to be right for stabs. FIXME. */
  1486. nm = debug_add_to_namespace (info, &info->current_file->globals, name,
  1487. DEBUG_OBJECT_TYPE, DEBUG_LINKAGE_NONE);
  1488. if (nm == NULL)
  1489. return DEBUG_TYPE_NULL;
  1490. nm->u.type = t;
  1491. n->name = nm;
  1492. return t;
  1493. }
  1494. /* Tag a type. */
  1495. debug_type
  1496. debug_tag_type (void *handle, const char *name, debug_type type)
  1497. {
  1498. struct debug_handle *info = (struct debug_handle *) handle;
  1499. struct debug_type_s *t;
  1500. struct debug_named_type *n;
  1501. struct debug_name *nm;
  1502. if (name == NULL || type == NULL)
  1503. return DEBUG_TYPE_NULL;
  1504. if (info->current_file == NULL)
  1505. {
  1506. debug_error (_("debug_tag_type: no current file"));
  1507. return DEBUG_TYPE_NULL;
  1508. }
  1509. if (type->kind == DEBUG_KIND_TAGGED)
  1510. {
  1511. if (strcmp (type->u.knamed->name->name, name) == 0)
  1512. return type;
  1513. debug_error (_("debug_tag_type: extra tag attempted"));
  1514. return DEBUG_TYPE_NULL;
  1515. }
  1516. t = debug_make_type (info, DEBUG_KIND_TAGGED, 0);
  1517. if (t == NULL)
  1518. return DEBUG_TYPE_NULL;
  1519. n = (struct debug_named_type *) xmalloc (sizeof *n);
  1520. memset (n, 0, sizeof *n);
  1521. n->type = type;
  1522. t->u.knamed = n;
  1523. /* We keep a global namespace of tags for each compilation unit. I
  1524. don't know if that is the right thing to do. */
  1525. nm = debug_add_to_namespace (info, &info->current_file->globals, name,
  1526. DEBUG_OBJECT_TAG, DEBUG_LINKAGE_NONE);
  1527. if (nm == NULL)
  1528. return DEBUG_TYPE_NULL;
  1529. nm->u.tag = t;
  1530. n->name = nm;
  1531. return t;
  1532. }
  1533. /* Record the size of a given type. */
  1534. bool
  1535. debug_record_type_size (void *handle ATTRIBUTE_UNUSED, debug_type type,
  1536. unsigned int size)
  1537. {
  1538. if (type->size != 0 && type->size != size)
  1539. fprintf (stderr, _("Warning: changing type size from %d to %d\n"),
  1540. type->size, size);
  1541. type->size = size;
  1542. return true;
  1543. }
  1544. /* Find a named type. */
  1545. debug_type
  1546. debug_find_named_type (void *handle, const char *name)
  1547. {
  1548. struct debug_handle *info = (struct debug_handle *) handle;
  1549. struct debug_block *b;
  1550. struct debug_file *f;
  1551. /* We only search the current compilation unit. I don't know if
  1552. this is right or not. */
  1553. if (info->current_unit == NULL)
  1554. {
  1555. debug_error (_("debug_find_named_type: no current compilation unit"));
  1556. return DEBUG_TYPE_NULL;
  1557. }
  1558. for (b = info->current_block; b != NULL; b = b->parent)
  1559. {
  1560. if (b->locals != NULL)
  1561. {
  1562. struct debug_name *n;
  1563. for (n = b->locals->list; n != NULL; n = n->next)
  1564. {
  1565. if (n->kind == DEBUG_OBJECT_TYPE
  1566. && n->name[0] == name[0]
  1567. && strcmp (n->name, name) == 0)
  1568. return n->u.type;
  1569. }
  1570. }
  1571. }
  1572. for (f = info->current_unit->files; f != NULL; f = f->next)
  1573. {
  1574. if (f->globals != NULL)
  1575. {
  1576. struct debug_name *n;
  1577. for (n = f->globals->list; n != NULL; n = n->next)
  1578. {
  1579. if (n->kind == DEBUG_OBJECT_TYPE
  1580. && n->name[0] == name[0]
  1581. && strcmp (n->name, name) == 0)
  1582. return n->u.type;
  1583. }
  1584. }
  1585. }
  1586. return DEBUG_TYPE_NULL;
  1587. }
  1588. /* Find a tagged type. */
  1589. debug_type
  1590. debug_find_tagged_type (void *handle, const char *name,
  1591. enum debug_type_kind kind)
  1592. {
  1593. struct debug_handle *info = (struct debug_handle *) handle;
  1594. struct debug_unit *u;
  1595. /* We search the globals of all the compilation units. I don't know
  1596. if this is correct or not. It would be easy to change. */
  1597. for (u = info->units; u != NULL; u = u->next)
  1598. {
  1599. struct debug_file *f;
  1600. for (f = u->files; f != NULL; f = f->next)
  1601. {
  1602. struct debug_name *n;
  1603. if (f->globals != NULL)
  1604. {
  1605. for (n = f->globals->list; n != NULL; n = n->next)
  1606. {
  1607. if (n->kind == DEBUG_OBJECT_TAG
  1608. && (kind == DEBUG_KIND_ILLEGAL
  1609. || n->u.tag->kind == kind)
  1610. && n->name[0] == name[0]
  1611. && strcmp (n->name, name) == 0)
  1612. return n->u.tag;
  1613. }
  1614. }
  1615. }
  1616. }
  1617. return DEBUG_TYPE_NULL;
  1618. }
  1619. /* Get a base type. We build a linked list on the stack to avoid
  1620. crashing if the type is defined circularly. */
  1621. static struct debug_type_s *
  1622. debug_get_real_type (void *handle, debug_type type,
  1623. struct debug_type_real_list *list)
  1624. {
  1625. struct debug_type_real_list *l;
  1626. struct debug_type_real_list rl;
  1627. switch (type->kind)
  1628. {
  1629. default:
  1630. return type;
  1631. case DEBUG_KIND_INDIRECT:
  1632. case DEBUG_KIND_NAMED:
  1633. case DEBUG_KIND_TAGGED:
  1634. break;
  1635. }
  1636. for (l = list; l != NULL; l = l->next)
  1637. {
  1638. if (l->t == type || l == l->next)
  1639. {
  1640. fprintf (stderr,
  1641. _("debug_get_real_type: circular debug information for %s\n"),
  1642. debug_get_type_name (handle, type));
  1643. return NULL;
  1644. }
  1645. }
  1646. rl.next = list;
  1647. rl.t = type;
  1648. switch (type->kind)
  1649. {
  1650. /* The default case is just here to avoid warnings. */
  1651. default:
  1652. case DEBUG_KIND_INDIRECT:
  1653. /* A valid non-self-referencing indirect type. */
  1654. if (*type->u.kindirect->slot != NULL
  1655. && *type->u.kindirect->slot != type)
  1656. return debug_get_real_type (handle, *type->u.kindirect->slot, &rl);
  1657. return type;
  1658. case DEBUG_KIND_NAMED:
  1659. case DEBUG_KIND_TAGGED:
  1660. return debug_get_real_type (handle, type->u.knamed->type, &rl);
  1661. }
  1662. /*NOTREACHED*/
  1663. }
  1664. /* Get the kind of a type. */
  1665. enum debug_type_kind
  1666. debug_get_type_kind (void *handle, debug_type type)
  1667. {
  1668. if (type == NULL)
  1669. return DEBUG_KIND_ILLEGAL;
  1670. type = debug_get_real_type (handle, type, NULL);
  1671. if (type == NULL)
  1672. return DEBUG_KIND_ILLEGAL;
  1673. return type->kind;
  1674. }
  1675. /* Get the name of a type. */
  1676. const char *
  1677. debug_get_type_name (void *handle, debug_type type)
  1678. {
  1679. if (type->kind == DEBUG_KIND_INDIRECT)
  1680. {
  1681. /* A valid non-self-referencing indirect type. */
  1682. if (*type->u.kindirect->slot != NULL
  1683. && *type->u.kindirect->slot != type)
  1684. return debug_get_type_name (handle, *type->u.kindirect->slot);
  1685. return type->u.kindirect->tag;
  1686. }
  1687. if (type->kind == DEBUG_KIND_NAMED
  1688. || type->kind == DEBUG_KIND_TAGGED)
  1689. return type->u.knamed->name->name;
  1690. return NULL;
  1691. }
  1692. /* Get the size of a type. */
  1693. bfd_vma
  1694. debug_get_type_size (void *handle, debug_type type)
  1695. {
  1696. if (type == NULL)
  1697. return 0;
  1698. /* We don't call debug_get_real_type, because somebody might have
  1699. called debug_record_type_size on a named or indirect type. */
  1700. if (type->size != 0)
  1701. return type->size;
  1702. switch (type->kind)
  1703. {
  1704. default:
  1705. return 0;
  1706. case DEBUG_KIND_INDIRECT:
  1707. /* A valid non-self-referencing indirect type. */
  1708. if (*type->u.kindirect->slot != NULL
  1709. && *type->u.kindirect->slot != type)
  1710. return debug_get_type_size (handle, *type->u.kindirect->slot);
  1711. return 0;
  1712. case DEBUG_KIND_NAMED:
  1713. case DEBUG_KIND_TAGGED:
  1714. return debug_get_type_size (handle, type->u.knamed->type);
  1715. }
  1716. /*NOTREACHED*/
  1717. }
  1718. /* Get the return type of a function or method type. */
  1719. debug_type
  1720. debug_get_return_type (void *handle, debug_type type)
  1721. {
  1722. if (type == NULL)
  1723. return DEBUG_TYPE_NULL;
  1724. type = debug_get_real_type (handle, type, NULL);
  1725. if (type == NULL)
  1726. return DEBUG_TYPE_NULL;
  1727. switch (type->kind)
  1728. {
  1729. default:
  1730. return DEBUG_TYPE_NULL;
  1731. case DEBUG_KIND_FUNCTION:
  1732. return type->u.kfunction->return_type;
  1733. case DEBUG_KIND_METHOD:
  1734. return type->u.kmethod->return_type;
  1735. }
  1736. /*NOTREACHED*/
  1737. }
  1738. /* Get the parameter types of a function or method type (except that
  1739. we don't currently store the parameter types of a function). */
  1740. const debug_type *
  1741. debug_get_parameter_types (void *handle, debug_type type,
  1742. bool *pvarargs)
  1743. {
  1744. if (type == NULL)
  1745. return NULL;
  1746. type = debug_get_real_type (handle, type, NULL);
  1747. if (type == NULL)
  1748. return NULL;
  1749. switch (type->kind)
  1750. {
  1751. default:
  1752. return NULL;
  1753. case DEBUG_KIND_FUNCTION:
  1754. *pvarargs = type->u.kfunction->varargs;
  1755. return type->u.kfunction->arg_types;
  1756. case DEBUG_KIND_METHOD:
  1757. *pvarargs = type->u.kmethod->varargs;
  1758. return type->u.kmethod->arg_types;
  1759. }
  1760. /*NOTREACHED*/
  1761. }
  1762. /* Get the target type of a type. */
  1763. debug_type
  1764. debug_get_target_type (void *handle, debug_type type)
  1765. {
  1766. if (type == NULL)
  1767. return NULL;
  1768. type = debug_get_real_type (handle, type, NULL);
  1769. if (type == NULL)
  1770. return NULL;
  1771. switch (type->kind)
  1772. {
  1773. default:
  1774. return NULL;
  1775. case DEBUG_KIND_POINTER:
  1776. return type->u.kpointer;
  1777. case DEBUG_KIND_REFERENCE:
  1778. return type->u.kreference;
  1779. case DEBUG_KIND_CONST:
  1780. return type->u.kconst;
  1781. case DEBUG_KIND_VOLATILE:
  1782. return type->u.kvolatile;
  1783. }
  1784. /*NOTREACHED*/
  1785. }
  1786. /* Get the NULL terminated array of fields for a struct, union, or
  1787. class. */
  1788. const debug_field *
  1789. debug_get_fields (void *handle, debug_type type)
  1790. {
  1791. if (type == NULL)
  1792. return NULL;
  1793. type = debug_get_real_type (handle, type, NULL);
  1794. if (type == NULL)
  1795. return NULL;
  1796. switch (type->kind)
  1797. {
  1798. default:
  1799. return NULL;
  1800. case DEBUG_KIND_STRUCT:
  1801. case DEBUG_KIND_UNION:
  1802. case DEBUG_KIND_CLASS:
  1803. case DEBUG_KIND_UNION_CLASS:
  1804. return type->u.kclass->fields;
  1805. }
  1806. /*NOTREACHED*/
  1807. }
  1808. /* Get the type of a field. */
  1809. debug_type
  1810. debug_get_field_type (void *handle ATTRIBUTE_UNUSED, debug_field field)
  1811. {
  1812. if (field == NULL)
  1813. return NULL;
  1814. return field->type;
  1815. }
  1816. /* Get the name of a field. */
  1817. const char *
  1818. debug_get_field_name (void *handle ATTRIBUTE_UNUSED, debug_field field)
  1819. {
  1820. if (field == NULL)
  1821. return NULL;
  1822. return field->name;
  1823. }
  1824. /* Get the bit position of a field. */
  1825. bfd_vma
  1826. debug_get_field_bitpos (void *handle ATTRIBUTE_UNUSED, debug_field field)
  1827. {
  1828. if (field == NULL || field->static_member)
  1829. return (bfd_vma) -1;
  1830. return field->u.f.bitpos;
  1831. }
  1832. /* Get the bit size of a field. */
  1833. bfd_vma
  1834. debug_get_field_bitsize (void *handle ATTRIBUTE_UNUSED, debug_field field)
  1835. {
  1836. if (field == NULL || field->static_member)
  1837. return (bfd_vma) -1;
  1838. return field->u.f.bitsize;
  1839. }
  1840. /* Get the visibility of a field. */
  1841. enum debug_visibility
  1842. debug_get_field_visibility (void *handle ATTRIBUTE_UNUSED, debug_field field)
  1843. {
  1844. if (field == NULL)
  1845. return DEBUG_VISIBILITY_IGNORE;
  1846. return field->visibility;
  1847. }
  1848. /* Get the physical name of a field. */
  1849. const char *
  1850. debug_get_field_physname (void *handle ATTRIBUTE_UNUSED, debug_field field)
  1851. {
  1852. if (field == NULL || ! field->static_member)
  1853. return NULL;
  1854. return field->u.s.physname;
  1855. }
  1856. /* Write out the debugging information. This is given a handle to
  1857. debugging information, and a set of function pointers to call. */
  1858. bool
  1859. debug_write (void *handle, const struct debug_write_fns *fns, void *fhandle)
  1860. {
  1861. struct debug_handle *info = (struct debug_handle *) handle;
  1862. struct debug_unit *u;
  1863. /* We use a mark to tell whether we have already written out a
  1864. particular name. We use an integer, so that we don't have to
  1865. clear the mark fields if we happen to write out the same
  1866. information more than once. */
  1867. ++info->mark;
  1868. /* The base_id field holds an ID value which will never be used, so
  1869. that we can tell whether we have assigned an ID during this call
  1870. to debug_write. */
  1871. info->base_id = info->class_id;
  1872. /* We keep a linked list of classes for which was have assigned ID's
  1873. during this call to debug_write. */
  1874. info->id_list = NULL;
  1875. for (u = info->units; u != NULL; u = u->next)
  1876. {
  1877. struct debug_file *f;
  1878. bool first_file;
  1879. info->current_write_lineno = u->linenos;
  1880. info->current_write_lineno_index = 0;
  1881. if (! (*fns->start_compilation_unit) (fhandle, u->files->filename))
  1882. return false;
  1883. first_file = true;
  1884. for (f = u->files; f != NULL; f = f->next)
  1885. {
  1886. struct debug_name *n;
  1887. if (first_file)
  1888. first_file = false;
  1889. else if (! (*fns->start_source) (fhandle, f->filename))
  1890. return false;
  1891. if (f->globals != NULL)
  1892. for (n = f->globals->list; n != NULL; n = n->next)
  1893. if (! debug_write_name (info, fns, fhandle, n))
  1894. return false;
  1895. }
  1896. /* Output any line number information which hasn't already been
  1897. handled. */
  1898. if (! debug_write_linenos (info, fns, fhandle, (bfd_vma) -1))
  1899. return false;
  1900. }
  1901. return true;
  1902. }
  1903. /* Write out an element in a namespace. */
  1904. static bool
  1905. debug_write_name (struct debug_handle *info,
  1906. const struct debug_write_fns *fns, void *fhandle,
  1907. struct debug_name *n)
  1908. {
  1909. switch (n->kind)
  1910. {
  1911. case DEBUG_OBJECT_TYPE:
  1912. if (! debug_write_type (info, fns, fhandle, n->u.type, n)
  1913. || ! (*fns->typdef) (fhandle, n->name))
  1914. return false;
  1915. return true;
  1916. case DEBUG_OBJECT_TAG:
  1917. if (! debug_write_type (info, fns, fhandle, n->u.tag, n))
  1918. return false;
  1919. return (*fns->tag) (fhandle, n->name);
  1920. case DEBUG_OBJECT_VARIABLE:
  1921. if (! debug_write_type (info, fns, fhandle, n->u.variable->type,
  1922. (struct debug_name *) NULL))
  1923. return false;
  1924. return (*fns->variable) (fhandle, n->name, n->u.variable->kind,
  1925. n->u.variable->val);
  1926. case DEBUG_OBJECT_FUNCTION:
  1927. return debug_write_function (info, fns, fhandle, n->name,
  1928. n->linkage, n->u.function);
  1929. case DEBUG_OBJECT_INT_CONSTANT:
  1930. return (*fns->int_constant) (fhandle, n->name, n->u.int_constant);
  1931. case DEBUG_OBJECT_FLOAT_CONSTANT:
  1932. return (*fns->float_constant) (fhandle, n->name, n->u.float_constant);
  1933. case DEBUG_OBJECT_TYPED_CONSTANT:
  1934. if (! debug_write_type (info, fns, fhandle, n->u.typed_constant->type,
  1935. (struct debug_name *) NULL))
  1936. return false;
  1937. return (*fns->typed_constant) (fhandle, n->name,
  1938. n->u.typed_constant->val);
  1939. default:
  1940. abort ();
  1941. return false;
  1942. }
  1943. /*NOTREACHED*/
  1944. }
  1945. /* Write out a type. If the type is DEBUG_KIND_NAMED or
  1946. DEBUG_KIND_TAGGED, then the name argument is the name for which we
  1947. are about to call typedef or tag. If the type is anything else,
  1948. then the name argument is a tag from a DEBUG_KIND_TAGGED type which
  1949. points to this one. */
  1950. static bool
  1951. debug_write_type (struct debug_handle *info,
  1952. const struct debug_write_fns *fns, void *fhandle,
  1953. struct debug_type_s *type, struct debug_name *name)
  1954. {
  1955. unsigned int i;
  1956. int is;
  1957. const char *tag = NULL;
  1958. if (type == DEBUG_TYPE_NULL)
  1959. return (*fns->empty_type) (fhandle);
  1960. /* If we have a name for this type, just output it. We only output
  1961. typedef names after they have been defined. We output type tags
  1962. whenever we are not actually defining them. */
  1963. if ((type->kind == DEBUG_KIND_NAMED
  1964. || type->kind == DEBUG_KIND_TAGGED)
  1965. && (type->u.knamed->name->mark == info->mark
  1966. || (type->kind == DEBUG_KIND_TAGGED
  1967. && type->u.knamed->name != name)))
  1968. {
  1969. if (type->kind == DEBUG_KIND_NAMED)
  1970. return (*fns->typedef_type) (fhandle, type->u.knamed->name->name);
  1971. else
  1972. {
  1973. struct debug_type_s *real;
  1974. unsigned int id;
  1975. real = debug_get_real_type ((void *) info, type, NULL);
  1976. if (real == NULL)
  1977. return (*fns->empty_type) (fhandle);
  1978. id = 0;
  1979. if ((real->kind == DEBUG_KIND_STRUCT
  1980. || real->kind == DEBUG_KIND_UNION
  1981. || real->kind == DEBUG_KIND_CLASS
  1982. || real->kind == DEBUG_KIND_UNION_CLASS)
  1983. && real->u.kclass != NULL)
  1984. {
  1985. if (real->u.kclass->id <= info->base_id)
  1986. {
  1987. if (! debug_set_class_id (info,
  1988. type->u.knamed->name->name,
  1989. real))
  1990. return false;
  1991. }
  1992. id = real->u.kclass->id;
  1993. }
  1994. return (*fns->tag_type) (fhandle, type->u.knamed->name->name, id,
  1995. real->kind);
  1996. }
  1997. }
  1998. /* Mark the name after we have already looked for a known name, so
  1999. that we don't just define a type in terms of itself. We need to
  2000. mark the name here so that a struct containing a pointer to
  2001. itself will work. */
  2002. if (name != NULL)
  2003. name->mark = info->mark;
  2004. if (name != NULL
  2005. && type->kind != DEBUG_KIND_NAMED
  2006. && type->kind != DEBUG_KIND_TAGGED)
  2007. {
  2008. assert (name->kind == DEBUG_OBJECT_TAG);
  2009. tag = name->name;
  2010. }
  2011. switch (type->kind)
  2012. {
  2013. case DEBUG_KIND_ILLEGAL:
  2014. debug_error (_("debug_write_type: illegal type encountered"));
  2015. return false;
  2016. case DEBUG_KIND_INDIRECT:
  2017. /* Prevent infinite recursion. */
  2018. if (*type->u.kindirect->slot == type)
  2019. return (*fns->empty_type) (fhandle);
  2020. return debug_write_type (info, fns, fhandle, *type->u.kindirect->slot,
  2021. name);
  2022. case DEBUG_KIND_VOID:
  2023. return (*fns->void_type) (fhandle);
  2024. case DEBUG_KIND_INT:
  2025. return (*fns->int_type) (fhandle, type->size, type->u.kint);
  2026. case DEBUG_KIND_FLOAT:
  2027. return (*fns->float_type) (fhandle, type->size);
  2028. case DEBUG_KIND_COMPLEX:
  2029. return (*fns->complex_type) (fhandle, type->size);
  2030. case DEBUG_KIND_BOOL:
  2031. return (*fns->bool_type) (fhandle, type->size);
  2032. case DEBUG_KIND_STRUCT:
  2033. case DEBUG_KIND_UNION:
  2034. if (type->u.kclass != NULL)
  2035. {
  2036. if (type->u.kclass->id <= info->base_id)
  2037. {
  2038. if (! debug_set_class_id (info, tag, type))
  2039. return false;
  2040. }
  2041. if (info->mark == type->u.kclass->mark)
  2042. {
  2043. /* We are currently outputting this struct, or we have
  2044. already output it. I don't know if this can happen,
  2045. but it can happen for a class. */
  2046. assert (type->u.kclass->id > info->base_id);
  2047. return (*fns->tag_type) (fhandle, tag, type->u.kclass->id,
  2048. type->kind);
  2049. }
  2050. type->u.kclass->mark = info->mark;
  2051. }
  2052. if (! (*fns->start_struct_type) (fhandle, tag,
  2053. (type->u.kclass != NULL
  2054. ? type->u.kclass->id
  2055. : 0),
  2056. type->kind == DEBUG_KIND_STRUCT,
  2057. type->size))
  2058. return false;
  2059. if (type->u.kclass != NULL
  2060. && type->u.kclass->fields != NULL)
  2061. {
  2062. for (i = 0; type->u.kclass->fields[i] != NULL; i++)
  2063. {
  2064. struct debug_field_s *f;
  2065. f = type->u.kclass->fields[i];
  2066. if (! debug_write_type (info, fns, fhandle, f->type,
  2067. (struct debug_name *) NULL)
  2068. || ! (*fns->struct_field) (fhandle, f->name, f->u.f.bitpos,
  2069. f->u.f.bitsize, f->visibility))
  2070. return false;
  2071. }
  2072. }
  2073. return (*fns->end_struct_type) (fhandle);
  2074. case DEBUG_KIND_CLASS:
  2075. case DEBUG_KIND_UNION_CLASS:
  2076. return debug_write_class_type (info, fns, fhandle, type, tag);
  2077. case DEBUG_KIND_ENUM:
  2078. if (type->u.kenum == NULL)
  2079. return (*fns->enum_type) (fhandle, tag, (const char **) NULL,
  2080. (bfd_signed_vma *) NULL);
  2081. return (*fns->enum_type) (fhandle, tag, type->u.kenum->names,
  2082. type->u.kenum->values);
  2083. case DEBUG_KIND_POINTER:
  2084. if (! debug_write_type (info, fns, fhandle, type->u.kpointer,
  2085. (struct debug_name *) NULL))
  2086. return false;
  2087. return (*fns->pointer_type) (fhandle);
  2088. case DEBUG_KIND_FUNCTION:
  2089. if (! debug_write_type (info, fns, fhandle,
  2090. type->u.kfunction->return_type,
  2091. (struct debug_name *) NULL))
  2092. return false;
  2093. if (type->u.kfunction->arg_types == NULL)
  2094. is = -1;
  2095. else
  2096. {
  2097. for (is = 0; type->u.kfunction->arg_types[is] != NULL; is++)
  2098. if (! debug_write_type (info, fns, fhandle,
  2099. type->u.kfunction->arg_types[is],
  2100. (struct debug_name *) NULL))
  2101. return false;
  2102. }
  2103. return (*fns->function_type) (fhandle, is,
  2104. type->u.kfunction->varargs);
  2105. case DEBUG_KIND_REFERENCE:
  2106. if (! debug_write_type (info, fns, fhandle, type->u.kreference,
  2107. (struct debug_name *) NULL))
  2108. return false;
  2109. return (*fns->reference_type) (fhandle);
  2110. case DEBUG_KIND_RANGE:
  2111. if (! debug_write_type (info, fns, fhandle, type->u.krange->type,
  2112. (struct debug_name *) NULL))
  2113. return false;
  2114. return (*fns->range_type) (fhandle, type->u.krange->lower,
  2115. type->u.krange->upper);
  2116. case DEBUG_KIND_ARRAY:
  2117. if (! debug_write_type (info, fns, fhandle, type->u.karray->element_type,
  2118. (struct debug_name *) NULL)
  2119. || ! debug_write_type (info, fns, fhandle,
  2120. type->u.karray->range_type,
  2121. (struct debug_name *) NULL))
  2122. return false;
  2123. return (*fns->array_type) (fhandle, type->u.karray->lower,
  2124. type->u.karray->upper,
  2125. type->u.karray->stringp);
  2126. case DEBUG_KIND_SET:
  2127. if (! debug_write_type (info, fns, fhandle, type->u.kset->type,
  2128. (struct debug_name *) NULL))
  2129. return false;
  2130. return (*fns->set_type) (fhandle, type->u.kset->bitstringp);
  2131. case DEBUG_KIND_OFFSET:
  2132. if (! debug_write_type (info, fns, fhandle, type->u.koffset->base_type,
  2133. (struct debug_name *) NULL)
  2134. || ! debug_write_type (info, fns, fhandle,
  2135. type->u.koffset->target_type,
  2136. (struct debug_name *) NULL))
  2137. return false;
  2138. return (*fns->offset_type) (fhandle);
  2139. case DEBUG_KIND_METHOD:
  2140. if (! debug_write_type (info, fns, fhandle,
  2141. type->u.kmethod->return_type,
  2142. (struct debug_name *) NULL))
  2143. return false;
  2144. if (type->u.kmethod->arg_types == NULL)
  2145. is = -1;
  2146. else
  2147. {
  2148. for (is = 0; type->u.kmethod->arg_types[is] != NULL; is++)
  2149. if (! debug_write_type (info, fns, fhandle,
  2150. type->u.kmethod->arg_types[is],
  2151. (struct debug_name *) NULL))
  2152. return false;
  2153. }
  2154. if (type->u.kmethod->domain_type != NULL)
  2155. {
  2156. if (! debug_write_type (info, fns, fhandle,
  2157. type->u.kmethod->domain_type,
  2158. (struct debug_name *) NULL))
  2159. return false;
  2160. }
  2161. return (*fns->method_type) (fhandle,
  2162. type->u.kmethod->domain_type != NULL,
  2163. is,
  2164. type->u.kmethod->varargs);
  2165. case DEBUG_KIND_CONST:
  2166. if (! debug_write_type (info, fns, fhandle, type->u.kconst,
  2167. (struct debug_name *) NULL))
  2168. return false;
  2169. return (*fns->const_type) (fhandle);
  2170. case DEBUG_KIND_VOLATILE:
  2171. if (! debug_write_type (info, fns, fhandle, type->u.kvolatile,
  2172. (struct debug_name *) NULL))
  2173. return false;
  2174. return (*fns->volatile_type) (fhandle);
  2175. case DEBUG_KIND_NAMED:
  2176. return debug_write_type (info, fns, fhandle, type->u.knamed->type,
  2177. (struct debug_name *) NULL);
  2178. case DEBUG_KIND_TAGGED:
  2179. return debug_write_type (info, fns, fhandle, type->u.knamed->type,
  2180. type->u.knamed->name);
  2181. default:
  2182. abort ();
  2183. return false;
  2184. }
  2185. }
  2186. /* Write out a class type. */
  2187. static bool
  2188. debug_write_class_type (struct debug_handle *info,
  2189. const struct debug_write_fns *fns, void *fhandle,
  2190. struct debug_type_s *type, const char *tag)
  2191. {
  2192. unsigned int i;
  2193. unsigned int id;
  2194. struct debug_type_s *vptrbase;
  2195. if (type->u.kclass == NULL)
  2196. {
  2197. id = 0;
  2198. vptrbase = NULL;
  2199. }
  2200. else
  2201. {
  2202. if (type->u.kclass->id <= info->base_id)
  2203. {
  2204. if (! debug_set_class_id (info, tag, type))
  2205. return false;
  2206. }
  2207. if (info->mark == type->u.kclass->mark)
  2208. {
  2209. /* We are currently outputting this class, or we have
  2210. already output it. This can happen when there are
  2211. methods for an anonymous class. */
  2212. assert (type->u.kclass->id > info->base_id);
  2213. return (*fns->tag_type) (fhandle, tag, type->u.kclass->id,
  2214. type->kind);
  2215. }
  2216. type->u.kclass->mark = info->mark;
  2217. id = type->u.kclass->id;
  2218. vptrbase = type->u.kclass->vptrbase;
  2219. if (vptrbase != NULL && vptrbase != type)
  2220. {
  2221. if (! debug_write_type (info, fns, fhandle, vptrbase,
  2222. (struct debug_name *) NULL))
  2223. return false;
  2224. }
  2225. }
  2226. if (! (*fns->start_class_type) (fhandle, tag, id,
  2227. type->kind == DEBUG_KIND_CLASS,
  2228. type->size,
  2229. vptrbase != NULL,
  2230. vptrbase == type))
  2231. return false;
  2232. if (type->u.kclass != NULL)
  2233. {
  2234. if (type->u.kclass->fields != NULL)
  2235. {
  2236. for (i = 0; type->u.kclass->fields[i] != NULL; i++)
  2237. {
  2238. struct debug_field_s *f;
  2239. f = type->u.kclass->fields[i];
  2240. if (! debug_write_type (info, fns, fhandle, f->type,
  2241. (struct debug_name *) NULL))
  2242. return false;
  2243. if (f->static_member)
  2244. {
  2245. if (! (*fns->class_static_member) (fhandle, f->name,
  2246. f->u.s.physname,
  2247. f->visibility))
  2248. return false;
  2249. }
  2250. else
  2251. {
  2252. if (! (*fns->struct_field) (fhandle, f->name, f->u.f.bitpos,
  2253. f->u.f.bitsize, f->visibility))
  2254. return false;
  2255. }
  2256. }
  2257. }
  2258. if (type->u.kclass->baseclasses != NULL)
  2259. {
  2260. for (i = 0; type->u.kclass->baseclasses[i] != NULL; i++)
  2261. {
  2262. struct debug_baseclass_s *b;
  2263. b = type->u.kclass->baseclasses[i];
  2264. if (! debug_write_type (info, fns, fhandle, b->type,
  2265. (struct debug_name *) NULL))
  2266. return false;
  2267. if (! (*fns->class_baseclass) (fhandle, b->bitpos, b->is_virtual,
  2268. b->visibility))
  2269. return false;
  2270. }
  2271. }
  2272. if (type->u.kclass->methods != NULL)
  2273. {
  2274. for (i = 0; type->u.kclass->methods[i] != NULL; i++)
  2275. {
  2276. struct debug_method_s *m;
  2277. unsigned int j;
  2278. m = type->u.kclass->methods[i];
  2279. if (! (*fns->class_start_method) (fhandle, m->name))
  2280. return false;
  2281. for (j = 0; m->variants[j] != NULL; j++)
  2282. {
  2283. struct debug_method_variant_s *v;
  2284. v = m->variants[j];
  2285. if (v->context != NULL)
  2286. {
  2287. if (! debug_write_type (info, fns, fhandle, v->context,
  2288. (struct debug_name *) NULL))
  2289. return false;
  2290. }
  2291. if (! debug_write_type (info, fns, fhandle, v->type,
  2292. (struct debug_name *) NULL))
  2293. return false;
  2294. if (v->voffset != VOFFSET_STATIC_METHOD)
  2295. {
  2296. if (! (*fns->class_method_variant) (fhandle, v->physname,
  2297. v->visibility,
  2298. v->constp,
  2299. v->volatilep,
  2300. v->voffset,
  2301. v->context != NULL))
  2302. return false;
  2303. }
  2304. else
  2305. {
  2306. if (! (*fns->class_static_method_variant) (fhandle,
  2307. v->physname,
  2308. v->visibility,
  2309. v->constp,
  2310. v->volatilep))
  2311. return false;
  2312. }
  2313. }
  2314. if (! (*fns->class_end_method) (fhandle))
  2315. return false;
  2316. }
  2317. }
  2318. }
  2319. return (*fns->end_class_type) (fhandle);
  2320. }
  2321. /* Write out information for a function. */
  2322. static bool
  2323. debug_write_function (struct debug_handle *info,
  2324. const struct debug_write_fns *fns, void *fhandle,
  2325. const char *name, enum debug_object_linkage linkage,
  2326. struct debug_function *function)
  2327. {
  2328. struct debug_parameter *p;
  2329. struct debug_block *b;
  2330. if (! debug_write_linenos (info, fns, fhandle, function->blocks->start))
  2331. return false;
  2332. if (! debug_write_type (info, fns, fhandle, function->return_type,
  2333. (struct debug_name *) NULL))
  2334. return false;
  2335. if (! (*fns->start_function) (fhandle, name,
  2336. linkage == DEBUG_LINKAGE_GLOBAL))
  2337. return false;
  2338. for (p = function->parameters; p != NULL; p = p->next)
  2339. {
  2340. if (! debug_write_type (info, fns, fhandle, p->type,
  2341. (struct debug_name *) NULL)
  2342. || ! (*fns->function_parameter) (fhandle, p->name, p->kind, p->val))
  2343. return false;
  2344. }
  2345. for (b = function->blocks; b != NULL; b = b->next)
  2346. {
  2347. if (! debug_write_block (info, fns, fhandle, b))
  2348. return false;
  2349. }
  2350. return (*fns->end_function) (fhandle);
  2351. }
  2352. /* Write out information for a block. */
  2353. static bool
  2354. debug_write_block (struct debug_handle *info,
  2355. const struct debug_write_fns *fns, void *fhandle,
  2356. struct debug_block *block)
  2357. {
  2358. struct debug_name *n;
  2359. struct debug_block *b;
  2360. if (! debug_write_linenos (info, fns, fhandle, block->start))
  2361. return false;
  2362. /* I can't see any point to writing out a block with no local
  2363. variables, so we don't bother, except for the top level block. */
  2364. if (block->locals != NULL || block->parent == NULL)
  2365. {
  2366. if (! (*fns->start_block) (fhandle, block->start))
  2367. return false;
  2368. }
  2369. if (block->locals != NULL)
  2370. {
  2371. for (n = block->locals->list; n != NULL; n = n->next)
  2372. {
  2373. if (! debug_write_name (info, fns, fhandle, n))
  2374. return false;
  2375. }
  2376. }
  2377. for (b = block->children; b != NULL; b = b->next)
  2378. {
  2379. if (! debug_write_block (info, fns, fhandle, b))
  2380. return false;
  2381. }
  2382. if (! debug_write_linenos (info, fns, fhandle, block->end))
  2383. return false;
  2384. if (block->locals != NULL || block->parent == NULL)
  2385. {
  2386. if (! (*fns->end_block) (fhandle, block->end))
  2387. return false;
  2388. }
  2389. return true;
  2390. }
  2391. /* Write out line number information up to ADDRESS. */
  2392. static bool
  2393. debug_write_linenos (struct debug_handle *info,
  2394. const struct debug_write_fns *fns, void *fhandle,
  2395. bfd_vma address)
  2396. {
  2397. while (info->current_write_lineno != NULL)
  2398. {
  2399. struct debug_lineno *l;
  2400. l = info->current_write_lineno;
  2401. while (info->current_write_lineno_index < DEBUG_LINENO_COUNT)
  2402. {
  2403. if (l->linenos[info->current_write_lineno_index]
  2404. == (unsigned long) -1)
  2405. break;
  2406. if (l->addrs[info->current_write_lineno_index] >= address)
  2407. return true;
  2408. if (! (*fns->lineno) (fhandle, l->file->filename,
  2409. l->linenos[info->current_write_lineno_index],
  2410. l->addrs[info->current_write_lineno_index]))
  2411. return false;
  2412. ++info->current_write_lineno_index;
  2413. }
  2414. info->current_write_lineno = l->next;
  2415. info->current_write_lineno_index = 0;
  2416. }
  2417. return true;
  2418. }
  2419. /* Get the ID number for a class. If during the same call to
  2420. debug_write we find a struct with the same definition with the same
  2421. name, we use the same ID. This type of things happens because the
  2422. same struct will be defined by multiple compilation units. */
  2423. static bool
  2424. debug_set_class_id (struct debug_handle *info, const char *tag,
  2425. struct debug_type_s *type)
  2426. {
  2427. struct debug_class_type *c;
  2428. struct debug_class_id *l;
  2429. assert (type->kind == DEBUG_KIND_STRUCT
  2430. || type->kind == DEBUG_KIND_UNION
  2431. || type->kind == DEBUG_KIND_CLASS
  2432. || type->kind == DEBUG_KIND_UNION_CLASS);
  2433. c = type->u.kclass;
  2434. if (c->id > info->base_id)
  2435. return true;
  2436. for (l = info->id_list; l != NULL; l = l->next)
  2437. {
  2438. if (l->type->kind != type->kind)
  2439. continue;
  2440. if (tag == NULL)
  2441. {
  2442. if (l->tag != NULL)
  2443. continue;
  2444. }
  2445. else
  2446. {
  2447. if (l->tag == NULL
  2448. || l->tag[0] != tag[0]
  2449. || strcmp (l->tag, tag) != 0)
  2450. continue;
  2451. }
  2452. if (debug_type_samep (info, l->type, type))
  2453. {
  2454. c->id = l->type->u.kclass->id;
  2455. return true;
  2456. }
  2457. }
  2458. /* There are no identical types. Use a new ID, and add it to the
  2459. list. */
  2460. ++info->class_id;
  2461. c->id = info->class_id;
  2462. l = (struct debug_class_id *) xmalloc (sizeof *l);
  2463. memset (l, 0, sizeof *l);
  2464. l->type = type;
  2465. l->tag = tag;
  2466. l->next = info->id_list;
  2467. info->id_list = l;
  2468. return true;
  2469. }
  2470. /* See if two types are the same. At this point, we don't care about
  2471. tags and the like. */
  2472. static bool
  2473. debug_type_samep (struct debug_handle *info, struct debug_type_s *t1,
  2474. struct debug_type_s *t2)
  2475. {
  2476. struct debug_type_compare_list *l;
  2477. struct debug_type_compare_list top;
  2478. bool ret;
  2479. if (t1 == NULL)
  2480. return t2 == NULL;
  2481. if (t2 == NULL)
  2482. return false;
  2483. while (t1->kind == DEBUG_KIND_INDIRECT)
  2484. {
  2485. t1 = *t1->u.kindirect->slot;
  2486. if (t1 == NULL)
  2487. return false;
  2488. }
  2489. while (t2->kind == DEBUG_KIND_INDIRECT)
  2490. {
  2491. t2 = *t2->u.kindirect->slot;
  2492. if (t2 == NULL)
  2493. return false;
  2494. }
  2495. if (t1 == t2)
  2496. return true;
  2497. /* As a special case, permit a typedef to match a tag, since C++
  2498. debugging output will sometimes add a typedef where C debugging
  2499. output will not. */
  2500. if (t1->kind == DEBUG_KIND_NAMED
  2501. && t2->kind == DEBUG_KIND_TAGGED)
  2502. return debug_type_samep (info, t1->u.knamed->type, t2);
  2503. else if (t1->kind == DEBUG_KIND_TAGGED
  2504. && t2->kind == DEBUG_KIND_NAMED)
  2505. return debug_type_samep (info, t1, t2->u.knamed->type);
  2506. if (t1->kind != t2->kind
  2507. || t1->size != t2->size)
  2508. return false;
  2509. /* Get rid of the trivial cases first. */
  2510. switch (t1->kind)
  2511. {
  2512. default:
  2513. break;
  2514. case DEBUG_KIND_VOID:
  2515. case DEBUG_KIND_FLOAT:
  2516. case DEBUG_KIND_COMPLEX:
  2517. case DEBUG_KIND_BOOL:
  2518. return true;
  2519. case DEBUG_KIND_INT:
  2520. return t1->u.kint == t2->u.kint;
  2521. }
  2522. /* We have to avoid an infinite recursion. We do this by keeping a
  2523. list of types which we are comparing. We just keep the list on
  2524. the stack. If we encounter a pair of types we are currently
  2525. comparing, we just assume that they are equal. */
  2526. for (l = info->compare_list; l != NULL; l = l->next)
  2527. {
  2528. if (l->t1 == t1 && l->t2 == t2)
  2529. return true;
  2530. }
  2531. top.t1 = t1;
  2532. top.t2 = t2;
  2533. top.next = info->compare_list;
  2534. info->compare_list = &top;
  2535. switch (t1->kind)
  2536. {
  2537. default:
  2538. abort ();
  2539. ret = false;
  2540. break;
  2541. case DEBUG_KIND_STRUCT:
  2542. case DEBUG_KIND_UNION:
  2543. case DEBUG_KIND_CLASS:
  2544. case DEBUG_KIND_UNION_CLASS:
  2545. if (t1->u.kclass == NULL)
  2546. ret = t2->u.kclass == NULL;
  2547. else if (t2->u.kclass == NULL)
  2548. ret = false;
  2549. else if (t1->u.kclass->id > info->base_id
  2550. && t1->u.kclass->id == t2->u.kclass->id)
  2551. ret = true;
  2552. else
  2553. ret = debug_class_type_samep (info, t1, t2);
  2554. break;
  2555. case DEBUG_KIND_ENUM:
  2556. if (t1->u.kenum == NULL)
  2557. ret = t2->u.kenum == NULL;
  2558. else if (t2->u.kenum == NULL)
  2559. ret = false;
  2560. else
  2561. {
  2562. const char **pn1, **pn2;
  2563. bfd_signed_vma *pv1, *pv2;
  2564. pn1 = t1->u.kenum->names;
  2565. pn2 = t2->u.kenum->names;
  2566. pv1 = t1->u.kenum->values;
  2567. pv2 = t2->u.kenum->values;
  2568. while (*pn1 != NULL && *pn2 != NULL)
  2569. {
  2570. if (**pn1 != **pn2
  2571. || *pv1 != *pv2
  2572. || strcmp (*pn1, *pn2) != 0)
  2573. break;
  2574. ++pn1;
  2575. ++pn2;
  2576. ++pv1;
  2577. ++pv2;
  2578. }
  2579. ret = *pn1 == NULL && *pn2 == NULL;
  2580. }
  2581. break;
  2582. case DEBUG_KIND_POINTER:
  2583. ret = debug_type_samep (info, t1->u.kpointer, t2->u.kpointer);
  2584. break;
  2585. case DEBUG_KIND_FUNCTION:
  2586. if (t1->u.kfunction->varargs != t2->u.kfunction->varargs
  2587. || ! debug_type_samep (info, t1->u.kfunction->return_type,
  2588. t2->u.kfunction->return_type)
  2589. || ((t1->u.kfunction->arg_types == NULL)
  2590. != (t2->u.kfunction->arg_types == NULL)))
  2591. ret = false;
  2592. else if (t1->u.kfunction->arg_types == NULL)
  2593. ret = true;
  2594. else
  2595. {
  2596. struct debug_type_s **a1, **a2;
  2597. a1 = t1->u.kfunction->arg_types;
  2598. a2 = t2->u.kfunction->arg_types;
  2599. while (*a1 != NULL && *a2 != NULL)
  2600. {
  2601. if (! debug_type_samep (info, *a1, *a2))
  2602. break;
  2603. ++a1;
  2604. ++a2;
  2605. }
  2606. ret = *a1 == NULL && *a2 == NULL;
  2607. }
  2608. break;
  2609. case DEBUG_KIND_REFERENCE:
  2610. ret = debug_type_samep (info, t1->u.kreference, t2->u.kreference);
  2611. break;
  2612. case DEBUG_KIND_RANGE:
  2613. ret = (t1->u.krange->lower == t2->u.krange->lower
  2614. && t1->u.krange->upper == t2->u.krange->upper
  2615. && debug_type_samep (info, t1->u.krange->type,
  2616. t2->u.krange->type));
  2617. break;
  2618. case DEBUG_KIND_ARRAY:
  2619. ret = (t1->u.karray->lower == t2->u.karray->lower
  2620. && t1->u.karray->upper == t2->u.karray->upper
  2621. && t1->u.karray->stringp == t2->u.karray->stringp
  2622. && debug_type_samep (info, t1->u.karray->element_type,
  2623. t2->u.karray->element_type));
  2624. break;
  2625. case DEBUG_KIND_SET:
  2626. ret = (t1->u.kset->bitstringp == t2->u.kset->bitstringp
  2627. && debug_type_samep (info, t1->u.kset->type, t2->u.kset->type));
  2628. break;
  2629. case DEBUG_KIND_OFFSET:
  2630. ret = (debug_type_samep (info, t1->u.koffset->base_type,
  2631. t2->u.koffset->base_type)
  2632. && debug_type_samep (info, t1->u.koffset->target_type,
  2633. t2->u.koffset->target_type));
  2634. break;
  2635. case DEBUG_KIND_METHOD:
  2636. if (t1->u.kmethod->varargs != t2->u.kmethod->varargs
  2637. || ! debug_type_samep (info, t1->u.kmethod->return_type,
  2638. t2->u.kmethod->return_type)
  2639. || ! debug_type_samep (info, t1->u.kmethod->domain_type,
  2640. t2->u.kmethod->domain_type)
  2641. || ((t1->u.kmethod->arg_types == NULL)
  2642. != (t2->u.kmethod->arg_types == NULL)))
  2643. ret = false;
  2644. else if (t1->u.kmethod->arg_types == NULL)
  2645. ret = true;
  2646. else
  2647. {
  2648. struct debug_type_s **a1, **a2;
  2649. a1 = t1->u.kmethod->arg_types;
  2650. a2 = t2->u.kmethod->arg_types;
  2651. while (*a1 != NULL && *a2 != NULL)
  2652. {
  2653. if (! debug_type_samep (info, *a1, *a2))
  2654. break;
  2655. ++a1;
  2656. ++a2;
  2657. }
  2658. ret = *a1 == NULL && *a2 == NULL;
  2659. }
  2660. break;
  2661. case DEBUG_KIND_CONST:
  2662. ret = debug_type_samep (info, t1->u.kconst, t2->u.kconst);
  2663. break;
  2664. case DEBUG_KIND_VOLATILE:
  2665. ret = debug_type_samep (info, t1->u.kvolatile, t2->u.kvolatile);
  2666. break;
  2667. case DEBUG_KIND_NAMED:
  2668. case DEBUG_KIND_TAGGED:
  2669. ret = (strcmp (t1->u.knamed->name->name, t2->u.knamed->name->name) == 0
  2670. && debug_type_samep (info, t1->u.knamed->type,
  2671. t2->u.knamed->type));
  2672. break;
  2673. }
  2674. info->compare_list = top.next;
  2675. return ret;
  2676. }
  2677. /* See if two classes are the same. This is a subroutine of
  2678. debug_type_samep. */
  2679. static bool
  2680. debug_class_type_samep (struct debug_handle *info, struct debug_type_s *t1,
  2681. struct debug_type_s *t2)
  2682. {
  2683. struct debug_class_type *c1, *c2;
  2684. c1 = t1->u.kclass;
  2685. c2 = t2->u.kclass;
  2686. if ((c1->fields == NULL) != (c2->fields == NULL)
  2687. || (c1->baseclasses == NULL) != (c2->baseclasses == NULL)
  2688. || (c1->methods == NULL) != (c2->methods == NULL)
  2689. || (c1->vptrbase == NULL) != (c2->vptrbase == NULL))
  2690. return false;
  2691. if (c1->fields != NULL)
  2692. {
  2693. struct debug_field_s **pf1, **pf2;
  2694. for (pf1 = c1->fields, pf2 = c2->fields;
  2695. *pf1 != NULL && *pf2 != NULL;
  2696. pf1++, pf2++)
  2697. {
  2698. struct debug_field_s *f1, *f2;
  2699. f1 = *pf1;
  2700. f2 = *pf2;
  2701. if (f1->name[0] != f2->name[0]
  2702. || f1->visibility != f2->visibility
  2703. || f1->static_member != f2->static_member)
  2704. return false;
  2705. if (f1->static_member)
  2706. {
  2707. if (strcmp (f1->u.s.physname, f2->u.s.physname) != 0)
  2708. return false;
  2709. }
  2710. else
  2711. {
  2712. if (f1->u.f.bitpos != f2->u.f.bitpos
  2713. || f1->u.f.bitsize != f2->u.f.bitsize)
  2714. return false;
  2715. }
  2716. /* We do the checks which require function calls last. We
  2717. don't require that the types of fields have the same
  2718. names, since that sometimes fails in the presence of
  2719. typedefs and we really don't care. */
  2720. if (strcmp (f1->name, f2->name) != 0
  2721. || f1->type == NULL
  2722. || f2->type == NULL
  2723. || ! debug_type_samep (info,
  2724. debug_get_real_type ((void *) info,
  2725. f1->type, NULL),
  2726. debug_get_real_type ((void *) info,
  2727. f2->type, NULL)))
  2728. return false;
  2729. }
  2730. if (*pf1 != NULL || *pf2 != NULL)
  2731. return false;
  2732. }
  2733. if (c1->vptrbase != NULL)
  2734. {
  2735. if (! debug_type_samep (info, c1->vptrbase, c2->vptrbase))
  2736. return false;
  2737. }
  2738. if (c1->baseclasses != NULL)
  2739. {
  2740. struct debug_baseclass_s **pb1, **pb2;
  2741. for (pb1 = c1->baseclasses, pb2 = c2->baseclasses;
  2742. *pb1 != NULL && *pb2 != NULL;
  2743. ++pb1, ++pb2)
  2744. {
  2745. struct debug_baseclass_s *b1, *b2;
  2746. b1 = *pb1;
  2747. b2 = *pb2;
  2748. if (b1->bitpos != b2->bitpos
  2749. || b1->is_virtual != b2->is_virtual
  2750. || b1->visibility != b2->visibility
  2751. || ! debug_type_samep (info, b1->type, b2->type))
  2752. return false;
  2753. }
  2754. if (*pb1 != NULL || *pb2 != NULL)
  2755. return false;
  2756. }
  2757. if (c1->methods != NULL)
  2758. {
  2759. struct debug_method_s **pm1, **pm2;
  2760. for (pm1 = c1->methods, pm2 = c2->methods;
  2761. *pm1 != NULL && *pm2 != NULL;
  2762. ++pm1, ++pm2)
  2763. {
  2764. struct debug_method_s *m1, *m2;
  2765. m1 = *pm1;
  2766. m2 = *pm2;
  2767. if (m1->name[0] != m2->name[0]
  2768. || strcmp (m1->name, m2->name) != 0
  2769. || (m1->variants == NULL) != (m2->variants == NULL))
  2770. return false;
  2771. if (m1->variants != NULL)
  2772. {
  2773. struct debug_method_variant_s **pv1, **pv2;
  2774. for (pv1 = m1->variants, pv2 = m2->variants;
  2775. *pv1 != NULL && *pv2 != NULL;
  2776. ++pv1, ++pv2)
  2777. {
  2778. struct debug_method_variant_s *v1, *v2;
  2779. v1 = *pv1;
  2780. v2 = *pv2;
  2781. if (v1->physname[0] != v2->physname[0]
  2782. || v1->visibility != v2->visibility
  2783. || v1->constp != v2->constp
  2784. || v1->volatilep != v2->volatilep
  2785. || v1->voffset != v2->voffset
  2786. || (v1->context == NULL) != (v2->context == NULL)
  2787. || strcmp (v1->physname, v2->physname) != 0
  2788. || ! debug_type_samep (info, v1->type, v2->type))
  2789. return false;
  2790. if (v1->context != NULL)
  2791. {
  2792. if (! debug_type_samep (info, v1->context,
  2793. v2->context))
  2794. return false;
  2795. }
  2796. }
  2797. if (*pv1 != NULL || *pv2 != NULL)
  2798. return false;
  2799. }
  2800. }
  2801. if (*pm1 != NULL || *pm2 != NULL)
  2802. return false;
  2803. }
  2804. return true;
  2805. }