expr.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408
  1. /* DWARF 2 Expression Evaluator.
  2. Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. Contributed by Daniel Berlin (dan@dberlin.org)
  4. This file is part of GDB.
  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, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "block.h"
  17. #include "symtab.h"
  18. #include "gdbtypes.h"
  19. #include "value.h"
  20. #include "gdbcore.h"
  21. #include "dwarf2.h"
  22. #include "dwarf2/expr.h"
  23. #include "dwarf2/loc.h"
  24. #include "dwarf2/read.h"
  25. #include "frame.h"
  26. #include "gdbsupport/underlying.h"
  27. #include "gdbarch.h"
  28. #include "objfiles.h"
  29. /* Cookie for gdbarch data. */
  30. static struct gdbarch_data *dwarf_arch_cookie;
  31. /* This holds gdbarch-specific types used by the DWARF expression
  32. evaluator. See comments in execute_stack_op. */
  33. struct dwarf_gdbarch_types
  34. {
  35. struct type *dw_types[3];
  36. };
  37. /* Allocate and fill in dwarf_gdbarch_types for an arch. */
  38. static void *
  39. dwarf_gdbarch_types_init (struct gdbarch *gdbarch)
  40. {
  41. struct dwarf_gdbarch_types *types
  42. = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct dwarf_gdbarch_types);
  43. /* The types themselves are lazily initialized. */
  44. return types;
  45. }
  46. /* Ensure that a FRAME is defined, throw an exception otherwise. */
  47. static void
  48. ensure_have_frame (frame_info *frame, const char *op_name)
  49. {
  50. if (frame == nullptr)
  51. throw_error (GENERIC_ERROR,
  52. _("%s evaluation requires a frame."), op_name);
  53. }
  54. /* Ensure that a PER_CU is defined and throw an exception otherwise. */
  55. static void
  56. ensure_have_per_cu (dwarf2_per_cu_data *per_cu, const char* op_name)
  57. {
  58. if (per_cu == nullptr)
  59. throw_error (GENERIC_ERROR,
  60. _("%s evaluation requires a compilation unit."), op_name);
  61. }
  62. /* Return the number of bytes overlapping a contiguous chunk of N_BITS
  63. bits whose first bit is located at bit offset START. */
  64. static size_t
  65. bits_to_bytes (ULONGEST start, ULONGEST n_bits)
  66. {
  67. return (start % HOST_CHAR_BIT + n_bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
  68. }
  69. /* See expr.h. */
  70. CORE_ADDR
  71. read_addr_from_reg (frame_info *frame, int reg)
  72. {
  73. struct gdbarch *gdbarch = get_frame_arch (frame);
  74. int regnum = dwarf_reg_to_regnum_or_error (gdbarch, reg);
  75. return address_from_register (regnum, frame);
  76. }
  77. struct piece_closure
  78. {
  79. /* Reference count. */
  80. int refc = 0;
  81. /* The objfile from which this closure's expression came. */
  82. dwarf2_per_objfile *per_objfile = nullptr;
  83. /* The CU from which this closure's expression came. */
  84. dwarf2_per_cu_data *per_cu = nullptr;
  85. /* The pieces describing this variable. */
  86. std::vector<dwarf_expr_piece> pieces;
  87. /* Frame ID of frame to which a register value is relative, used
  88. only by DWARF_VALUE_REGISTER. */
  89. struct frame_id frame_id;
  90. };
  91. /* Allocate a closure for a value formed from separately-described
  92. PIECES. */
  93. static piece_closure *
  94. allocate_piece_closure (dwarf2_per_cu_data *per_cu,
  95. dwarf2_per_objfile *per_objfile,
  96. std::vector<dwarf_expr_piece> &&pieces,
  97. frame_info *frame)
  98. {
  99. piece_closure *c = new piece_closure;
  100. c->refc = 1;
  101. /* We must capture this here due to sharing of DWARF state. */
  102. c->per_objfile = per_objfile;
  103. c->per_cu = per_cu;
  104. c->pieces = std::move (pieces);
  105. if (frame == nullptr)
  106. c->frame_id = null_frame_id;
  107. else
  108. c->frame_id = get_frame_id (frame);
  109. for (dwarf_expr_piece &piece : c->pieces)
  110. if (piece.location == DWARF_VALUE_STACK)
  111. value_incref (piece.v.value);
  112. return c;
  113. }
  114. /* Read or write a pieced value V. If FROM != NULL, operate in "write
  115. mode": copy FROM into the pieces comprising V. If FROM == NULL,
  116. operate in "read mode": fetch the contents of the (lazy) value V by
  117. composing it from its pieces. If CHECK_OPTIMIZED is true, then no
  118. reading or writing is done; instead the return value of this
  119. function is true if any piece is optimized out. When
  120. CHECK_OPTIMIZED is true, FROM must be nullptr. */
  121. static bool
  122. rw_pieced_value (value *v, value *from, bool check_optimized)
  123. {
  124. int i;
  125. LONGEST offset = 0, max_offset;
  126. gdb_byte *v_contents;
  127. const gdb_byte *from_contents;
  128. piece_closure *c
  129. = (piece_closure *) value_computed_closure (v);
  130. gdb::byte_vector buffer;
  131. bool bits_big_endian = type_byte_order (value_type (v)) == BFD_ENDIAN_BIG;
  132. gdb_assert (!check_optimized || from == nullptr);
  133. if (from != nullptr)
  134. {
  135. from_contents = value_contents (from).data ();
  136. v_contents = nullptr;
  137. }
  138. else
  139. {
  140. if (value_type (v) != value_enclosing_type (v))
  141. internal_error (__FILE__, __LINE__,
  142. _("Should not be able to create a lazy value with "
  143. "an enclosing type"));
  144. if (check_optimized)
  145. v_contents = nullptr;
  146. else
  147. v_contents = value_contents_raw (v).data ();
  148. from_contents = nullptr;
  149. }
  150. ULONGEST bits_to_skip = 8 * value_offset (v);
  151. if (value_bitsize (v))
  152. {
  153. bits_to_skip += (8 * value_offset (value_parent (v))
  154. + value_bitpos (v));
  155. if (from != nullptr
  156. && (type_byte_order (value_type (from))
  157. == BFD_ENDIAN_BIG))
  158. {
  159. /* Use the least significant bits of FROM. */
  160. max_offset = 8 * TYPE_LENGTH (value_type (from));
  161. offset = max_offset - value_bitsize (v);
  162. }
  163. else
  164. max_offset = value_bitsize (v);
  165. }
  166. else
  167. max_offset = 8 * TYPE_LENGTH (value_type (v));
  168. /* Advance to the first non-skipped piece. */
  169. for (i = 0; i < c->pieces.size () && bits_to_skip >= c->pieces[i].size; i++)
  170. bits_to_skip -= c->pieces[i].size;
  171. for (; i < c->pieces.size () && offset < max_offset; i++)
  172. {
  173. dwarf_expr_piece *p = &c->pieces[i];
  174. size_t this_size_bits, this_size;
  175. this_size_bits = p->size - bits_to_skip;
  176. if (this_size_bits > max_offset - offset)
  177. this_size_bits = max_offset - offset;
  178. switch (p->location)
  179. {
  180. case DWARF_VALUE_REGISTER:
  181. {
  182. frame_info *frame = frame_find_by_id (c->frame_id);
  183. gdbarch *arch = get_frame_arch (frame);
  184. int gdb_regnum = dwarf_reg_to_regnum_or_error (arch, p->v.regno);
  185. ULONGEST reg_bits = 8 * register_size (arch, gdb_regnum);
  186. int optim, unavail;
  187. if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
  188. && p->offset + p->size < reg_bits)
  189. {
  190. /* Big-endian, and we want less than full size. */
  191. bits_to_skip += reg_bits - (p->offset + p->size);
  192. }
  193. else
  194. bits_to_skip += p->offset;
  195. this_size = bits_to_bytes (bits_to_skip, this_size_bits);
  196. buffer.resize (this_size);
  197. if (from == nullptr)
  198. {
  199. /* Read mode. */
  200. if (!get_frame_register_bytes (frame, gdb_regnum,
  201. bits_to_skip / 8,
  202. buffer, &optim, &unavail))
  203. {
  204. if (optim)
  205. {
  206. if (check_optimized)
  207. return true;
  208. mark_value_bits_optimized_out (v, offset,
  209. this_size_bits);
  210. }
  211. if (unavail && !check_optimized)
  212. mark_value_bits_unavailable (v, offset,
  213. this_size_bits);
  214. break;
  215. }
  216. if (!check_optimized)
  217. copy_bitwise (v_contents, offset,
  218. buffer.data (), bits_to_skip % 8,
  219. this_size_bits, bits_big_endian);
  220. }
  221. else
  222. {
  223. /* Write mode. */
  224. if (bits_to_skip % 8 != 0 || this_size_bits % 8 != 0)
  225. {
  226. /* Data is copied non-byte-aligned into the register.
  227. Need some bits from original register value. */
  228. get_frame_register_bytes (frame, gdb_regnum,
  229. bits_to_skip / 8,
  230. buffer, &optim, &unavail);
  231. if (optim)
  232. throw_error (OPTIMIZED_OUT_ERROR,
  233. _("Can't do read-modify-write to "
  234. "update bitfield; containing word "
  235. "has been optimized out"));
  236. if (unavail)
  237. throw_error (NOT_AVAILABLE_ERROR,
  238. _("Can't do read-modify-write to "
  239. "update bitfield; containing word "
  240. "is unavailable"));
  241. }
  242. copy_bitwise (buffer.data (), bits_to_skip % 8,
  243. from_contents, offset,
  244. this_size_bits, bits_big_endian);
  245. put_frame_register_bytes (frame, gdb_regnum,
  246. bits_to_skip / 8,
  247. buffer);
  248. }
  249. }
  250. break;
  251. case DWARF_VALUE_MEMORY:
  252. {
  253. if (check_optimized)
  254. break;
  255. bits_to_skip += p->offset;
  256. CORE_ADDR start_addr = p->v.mem.addr + bits_to_skip / 8;
  257. if (bits_to_skip % 8 == 0 && this_size_bits % 8 == 0
  258. && offset % 8 == 0)
  259. {
  260. /* Everything is byte-aligned; no buffer needed. */
  261. if (from != nullptr)
  262. write_memory_with_notification (start_addr,
  263. (from_contents
  264. + offset / 8),
  265. this_size_bits / 8);
  266. else
  267. read_value_memory (v, offset,
  268. p->v.mem.in_stack_memory,
  269. p->v.mem.addr + bits_to_skip / 8,
  270. v_contents + offset / 8,
  271. this_size_bits / 8);
  272. break;
  273. }
  274. this_size = bits_to_bytes (bits_to_skip, this_size_bits);
  275. buffer.resize (this_size);
  276. if (from == nullptr)
  277. {
  278. /* Read mode. */
  279. read_value_memory (v, offset,
  280. p->v.mem.in_stack_memory,
  281. p->v.mem.addr + bits_to_skip / 8,
  282. buffer.data (), this_size);
  283. copy_bitwise (v_contents, offset,
  284. buffer.data (), bits_to_skip % 8,
  285. this_size_bits, bits_big_endian);
  286. }
  287. else
  288. {
  289. /* Write mode. */
  290. if (bits_to_skip % 8 != 0 || this_size_bits % 8 != 0)
  291. {
  292. if (this_size <= 8)
  293. {
  294. /* Perform a single read for small sizes. */
  295. read_memory (start_addr, buffer.data (),
  296. this_size);
  297. }
  298. else
  299. {
  300. /* Only the first and last bytes can possibly have
  301. any bits reused. */
  302. read_memory (start_addr, buffer.data (), 1);
  303. read_memory (start_addr + this_size - 1,
  304. &buffer[this_size - 1], 1);
  305. }
  306. }
  307. copy_bitwise (buffer.data (), bits_to_skip % 8,
  308. from_contents, offset,
  309. this_size_bits, bits_big_endian);
  310. write_memory_with_notification (start_addr,
  311. buffer.data (),
  312. this_size);
  313. }
  314. }
  315. break;
  316. case DWARF_VALUE_STACK:
  317. {
  318. if (check_optimized)
  319. break;
  320. if (from != nullptr)
  321. {
  322. mark_value_bits_optimized_out (v, offset, this_size_bits);
  323. break;
  324. }
  325. gdbarch *objfile_gdbarch = c->per_objfile->objfile->arch ();
  326. ULONGEST stack_value_size_bits
  327. = 8 * TYPE_LENGTH (value_type (p->v.value));
  328. /* Use zeroes if piece reaches beyond stack value. */
  329. if (p->offset + p->size > stack_value_size_bits)
  330. break;
  331. /* Piece is anchored at least significant bit end. */
  332. if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
  333. bits_to_skip += stack_value_size_bits - p->offset - p->size;
  334. else
  335. bits_to_skip += p->offset;
  336. copy_bitwise (v_contents, offset,
  337. value_contents_all (p->v.value).data (),
  338. bits_to_skip,
  339. this_size_bits, bits_big_endian);
  340. }
  341. break;
  342. case DWARF_VALUE_LITERAL:
  343. {
  344. if (check_optimized)
  345. break;
  346. if (from != nullptr)
  347. {
  348. mark_value_bits_optimized_out (v, offset, this_size_bits);
  349. break;
  350. }
  351. ULONGEST literal_size_bits = 8 * p->v.literal.length;
  352. size_t n = this_size_bits;
  353. /* Cut off at the end of the implicit value. */
  354. bits_to_skip += p->offset;
  355. if (bits_to_skip >= literal_size_bits)
  356. break;
  357. if (n > literal_size_bits - bits_to_skip)
  358. n = literal_size_bits - bits_to_skip;
  359. copy_bitwise (v_contents, offset,
  360. p->v.literal.data, bits_to_skip,
  361. n, bits_big_endian);
  362. }
  363. break;
  364. case DWARF_VALUE_IMPLICIT_POINTER:
  365. if (from != nullptr)
  366. {
  367. mark_value_bits_optimized_out (v, offset, this_size_bits);
  368. break;
  369. }
  370. /* These bits show up as zeros -- but do not cause the value to
  371. be considered optimized-out. */
  372. break;
  373. case DWARF_VALUE_OPTIMIZED_OUT:
  374. if (check_optimized)
  375. return true;
  376. mark_value_bits_optimized_out (v, offset, this_size_bits);
  377. break;
  378. default:
  379. internal_error (__FILE__, __LINE__, _("invalid location type"));
  380. }
  381. offset += this_size_bits;
  382. bits_to_skip = 0;
  383. }
  384. return false;
  385. }
  386. static void
  387. read_pieced_value (value *v)
  388. {
  389. rw_pieced_value (v, nullptr, false);
  390. }
  391. static void
  392. write_pieced_value (value *to, value *from)
  393. {
  394. rw_pieced_value (to, from, false);
  395. }
  396. static bool
  397. is_optimized_out_pieced_value (value *v)
  398. {
  399. return rw_pieced_value (v, nullptr, true);
  400. }
  401. /* An implementation of an lval_funcs method to see whether a value is
  402. a synthetic pointer. */
  403. static int
  404. check_pieced_synthetic_pointer (const value *value, LONGEST bit_offset,
  405. int bit_length)
  406. {
  407. piece_closure *c = (piece_closure *) value_computed_closure (value);
  408. int i;
  409. bit_offset += 8 * value_offset (value);
  410. if (value_bitsize (value))
  411. bit_offset += value_bitpos (value);
  412. for (i = 0; i < c->pieces.size () && bit_length > 0; i++)
  413. {
  414. dwarf_expr_piece *p = &c->pieces[i];
  415. size_t this_size_bits = p->size;
  416. if (bit_offset > 0)
  417. {
  418. if (bit_offset >= this_size_bits)
  419. {
  420. bit_offset -= this_size_bits;
  421. continue;
  422. }
  423. bit_length -= this_size_bits - bit_offset;
  424. bit_offset = 0;
  425. }
  426. else
  427. bit_length -= this_size_bits;
  428. if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
  429. return 0;
  430. }
  431. return 1;
  432. }
  433. /* An implementation of an lval_funcs method to indirect through a
  434. pointer. This handles the synthetic pointer case when needed. */
  435. static value *
  436. indirect_pieced_value (value *value)
  437. {
  438. piece_closure *c
  439. = (piece_closure *) value_computed_closure (value);
  440. int i;
  441. dwarf_expr_piece *piece = NULL;
  442. struct type *type = check_typedef (value_type (value));
  443. if (type->code () != TYPE_CODE_PTR)
  444. return NULL;
  445. int bit_length = 8 * TYPE_LENGTH (type);
  446. LONGEST bit_offset = 8 * value_offset (value);
  447. if (value_bitsize (value))
  448. bit_offset += value_bitpos (value);
  449. for (i = 0; i < c->pieces.size () && bit_length > 0; i++)
  450. {
  451. dwarf_expr_piece *p = &c->pieces[i];
  452. size_t this_size_bits = p->size;
  453. if (bit_offset > 0)
  454. {
  455. if (bit_offset >= this_size_bits)
  456. {
  457. bit_offset -= this_size_bits;
  458. continue;
  459. }
  460. bit_length -= this_size_bits - bit_offset;
  461. bit_offset = 0;
  462. }
  463. else
  464. bit_length -= this_size_bits;
  465. if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
  466. return NULL;
  467. if (bit_length != 0)
  468. error (_("Invalid use of DW_OP_implicit_pointer"));
  469. piece = p;
  470. break;
  471. }
  472. gdb_assert (piece != NULL && c->per_cu != nullptr);
  473. frame_info *frame = get_selected_frame (_("No frame selected."));
  474. /* This is an offset requested by GDB, such as value subscripts.
  475. However, due to how synthetic pointers are implemented, this is
  476. always presented to us as a pointer type. This means we have to
  477. sign-extend it manually as appropriate. Use raw
  478. extract_signed_integer directly rather than value_as_address and
  479. sign extend afterwards on architectures that would need it
  480. (mostly everywhere except MIPS, which has signed addresses) as
  481. the later would go through gdbarch_pointer_to_address and thus
  482. return a CORE_ADDR with high bits set on architectures that
  483. encode address spaces and other things in CORE_ADDR. */
  484. bfd_endian byte_order = gdbarch_byte_order (get_frame_arch (frame));
  485. LONGEST byte_offset
  486. = extract_signed_integer (value_contents (value), byte_order);
  487. byte_offset += piece->v.ptr.offset;
  488. return indirect_synthetic_pointer (piece->v.ptr.die_sect_off,
  489. byte_offset, c->per_cu,
  490. c->per_objfile, frame, type);
  491. }
  492. /* Implementation of the coerce_ref method of lval_funcs for synthetic C++
  493. references. */
  494. static value *
  495. coerce_pieced_ref (const value *value)
  496. {
  497. struct type *type = check_typedef (value_type (value));
  498. if (value_bits_synthetic_pointer (value, value_embedded_offset (value),
  499. TARGET_CHAR_BIT * TYPE_LENGTH (type)))
  500. {
  501. const piece_closure *closure
  502. = (piece_closure *) value_computed_closure (value);
  503. frame_info *frame
  504. = get_selected_frame (_("No frame selected."));
  505. /* gdb represents synthetic pointers as pieced values with a single
  506. piece. */
  507. gdb_assert (closure != NULL);
  508. gdb_assert (closure->pieces.size () == 1);
  509. return indirect_synthetic_pointer
  510. (closure->pieces[0].v.ptr.die_sect_off,
  511. closure->pieces[0].v.ptr.offset,
  512. closure->per_cu, closure->per_objfile, frame, type);
  513. }
  514. else
  515. {
  516. /* Else: not a synthetic reference; do nothing. */
  517. return NULL;
  518. }
  519. }
  520. static void *
  521. copy_pieced_value_closure (const value *v)
  522. {
  523. piece_closure *c = (piece_closure *) value_computed_closure (v);
  524. ++c->refc;
  525. return c;
  526. }
  527. static void
  528. free_pieced_value_closure (value *v)
  529. {
  530. piece_closure *c = (piece_closure *) value_computed_closure (v);
  531. --c->refc;
  532. if (c->refc == 0)
  533. {
  534. for (dwarf_expr_piece &p : c->pieces)
  535. if (p.location == DWARF_VALUE_STACK)
  536. value_decref (p.v.value);
  537. delete c;
  538. }
  539. }
  540. /* Functions for accessing a variable described by DW_OP_piece. */
  541. static const struct lval_funcs pieced_value_funcs = {
  542. read_pieced_value,
  543. write_pieced_value,
  544. is_optimized_out_pieced_value,
  545. indirect_pieced_value,
  546. coerce_pieced_ref,
  547. check_pieced_synthetic_pointer,
  548. copy_pieced_value_closure,
  549. free_pieced_value_closure
  550. };
  551. /* Given context CTX, section offset SECT_OFF, and compilation unit
  552. data PER_CU, execute the "variable value" operation on the DIE
  553. found at SECT_OFF. */
  554. static value *
  555. sect_variable_value (sect_offset sect_off,
  556. dwarf2_per_cu_data *per_cu,
  557. dwarf2_per_objfile *per_objfile)
  558. {
  559. const char *var_name = nullptr;
  560. struct type *die_type
  561. = dwarf2_fetch_die_type_sect_off (sect_off, per_cu, per_objfile,
  562. &var_name);
  563. if (die_type == NULL)
  564. error (_("Bad DW_OP_GNU_variable_value DIE."));
  565. /* Note: Things still work when the following test is removed. This
  566. test and error is here to conform to the proposed specification. */
  567. if (die_type->code () != TYPE_CODE_INT
  568. && die_type->code () != TYPE_CODE_ENUM
  569. && die_type->code () != TYPE_CODE_RANGE
  570. && die_type->code () != TYPE_CODE_PTR)
  571. error (_("Type of DW_OP_GNU_variable_value DIE must be an integer or pointer."));
  572. if (var_name != nullptr)
  573. {
  574. value *result = compute_var_value (var_name);
  575. if (result != nullptr)
  576. return result;
  577. }
  578. struct type *type = lookup_pointer_type (die_type);
  579. frame_info *frame = get_selected_frame (_("No frame selected."));
  580. return indirect_synthetic_pointer (sect_off, 0, per_cu, per_objfile, frame,
  581. type, true);
  582. }
  583. /* Return the type used for DWARF operations where the type is
  584. unspecified in the DWARF spec. Only certain sizes are
  585. supported. */
  586. struct type *
  587. dwarf_expr_context::address_type () const
  588. {
  589. gdbarch *arch = this->m_per_objfile->objfile->arch ();
  590. dwarf_gdbarch_types *types
  591. = (dwarf_gdbarch_types *) gdbarch_data (arch, dwarf_arch_cookie);
  592. int ndx;
  593. if (this->m_addr_size == 2)
  594. ndx = 0;
  595. else if (this->m_addr_size == 4)
  596. ndx = 1;
  597. else if (this->m_addr_size == 8)
  598. ndx = 2;
  599. else
  600. error (_("Unsupported address size in DWARF expressions: %d bits"),
  601. 8 * this->m_addr_size);
  602. if (types->dw_types[ndx] == NULL)
  603. types->dw_types[ndx]
  604. = arch_integer_type (arch, 8 * this->m_addr_size,
  605. 0, "<signed DWARF address type>");
  606. return types->dw_types[ndx];
  607. }
  608. /* Create a new context for the expression evaluator. */
  609. dwarf_expr_context::dwarf_expr_context (dwarf2_per_objfile *per_objfile,
  610. int addr_size)
  611. : m_addr_size (addr_size),
  612. m_per_objfile (per_objfile)
  613. {
  614. }
  615. /* Push VALUE onto the stack. */
  616. void
  617. dwarf_expr_context::push (struct value *value, bool in_stack_memory)
  618. {
  619. this->m_stack.emplace_back (value, in_stack_memory);
  620. }
  621. /* Push VALUE onto the stack. */
  622. void
  623. dwarf_expr_context::push_address (CORE_ADDR value, bool in_stack_memory)
  624. {
  625. push (value_from_ulongest (address_type (), value), in_stack_memory);
  626. }
  627. /* Pop the top item off of the stack. */
  628. void
  629. dwarf_expr_context::pop ()
  630. {
  631. if (this->m_stack.empty ())
  632. error (_("dwarf expression stack underflow"));
  633. this->m_stack.pop_back ();
  634. }
  635. /* Retrieve the N'th item on the stack. */
  636. struct value *
  637. dwarf_expr_context::fetch (int n)
  638. {
  639. if (this->m_stack.size () <= n)
  640. error (_("Asked for position %d of stack, "
  641. "stack only has %zu elements on it."),
  642. n, this->m_stack.size ());
  643. return this->m_stack[this->m_stack.size () - (1 + n)].value;
  644. }
  645. /* See expr.h. */
  646. void
  647. dwarf_expr_context::get_frame_base (const gdb_byte **start,
  648. size_t * length)
  649. {
  650. ensure_have_frame (this->m_frame, "DW_OP_fbreg");
  651. const block *bl = get_frame_block (this->m_frame, NULL);
  652. if (bl == NULL)
  653. error (_("frame address is not available."));
  654. /* Use block_linkage_function, which returns a real (not inlined)
  655. function, instead of get_frame_function, which may return an
  656. inlined function. */
  657. symbol *framefunc = block_linkage_function (bl);
  658. /* If we found a frame-relative symbol then it was certainly within
  659. some function associated with a frame. If we can't find the frame,
  660. something has gone wrong. */
  661. gdb_assert (framefunc != NULL);
  662. func_get_frame_base_dwarf_block (framefunc,
  663. get_frame_address_in_block (this->m_frame),
  664. start, length);
  665. }
  666. /* See expr.h. */
  667. struct type *
  668. dwarf_expr_context::get_base_type (cu_offset die_cu_off)
  669. {
  670. if (this->m_per_cu == nullptr)
  671. return builtin_type (this->m_per_objfile->objfile->arch ())->builtin_int;
  672. struct type *result = dwarf2_get_die_type (die_cu_off, this->m_per_cu,
  673. this->m_per_objfile);
  674. if (result == nullptr)
  675. error (_("Could not find type for operation"));
  676. return result;
  677. }
  678. /* See expr.h. */
  679. void
  680. dwarf_expr_context::dwarf_call (cu_offset die_cu_off)
  681. {
  682. ensure_have_per_cu (this->m_per_cu, "DW_OP_call");
  683. frame_info *frame = this->m_frame;
  684. auto get_pc_from_frame = [frame] ()
  685. {
  686. ensure_have_frame (frame, "DW_OP_call");
  687. return get_frame_address_in_block (frame);
  688. };
  689. dwarf2_locexpr_baton block
  690. = dwarf2_fetch_die_loc_cu_off (die_cu_off, this->m_per_cu,
  691. this->m_per_objfile, get_pc_from_frame);
  692. /* DW_OP_call_ref is currently not supported. */
  693. gdb_assert (block.per_cu == this->m_per_cu);
  694. this->eval (block.data, block.size);
  695. }
  696. /* See expr.h. */
  697. void
  698. dwarf_expr_context::read_mem (gdb_byte *buf, CORE_ADDR addr,
  699. size_t length)
  700. {
  701. if (length == 0)
  702. return;
  703. /* Prefer the passed-in memory, if it exists. */
  704. if (this->m_addr_info != nullptr)
  705. {
  706. CORE_ADDR offset = addr - this->m_addr_info->addr;
  707. if (offset < this->m_addr_info->valaddr.size ()
  708. && offset + length <= this->m_addr_info->valaddr.size ())
  709. {
  710. memcpy (buf, this->m_addr_info->valaddr.data (), length);
  711. return;
  712. }
  713. }
  714. read_memory (addr, buf, length);
  715. }
  716. /* See expr.h. */
  717. void
  718. dwarf_expr_context::push_dwarf_reg_entry_value (call_site_parameter_kind kind,
  719. call_site_parameter_u kind_u,
  720. int deref_size)
  721. {
  722. ensure_have_per_cu (this->m_per_cu, "DW_OP_entry_value");
  723. ensure_have_frame (this->m_frame, "DW_OP_entry_value");
  724. dwarf2_per_cu_data *caller_per_cu;
  725. dwarf2_per_objfile *caller_per_objfile;
  726. frame_info *caller_frame = get_prev_frame (this->m_frame);
  727. call_site_parameter *parameter
  728. = dwarf_expr_reg_to_entry_parameter (this->m_frame, kind, kind_u,
  729. &caller_per_cu,
  730. &caller_per_objfile);
  731. const gdb_byte *data_src
  732. = deref_size == -1 ? parameter->value : parameter->data_value;
  733. size_t size
  734. = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
  735. /* DEREF_SIZE size is not verified here. */
  736. if (data_src == nullptr)
  737. throw_error (NO_ENTRY_VALUE_ERROR,
  738. _("Cannot resolve DW_AT_call_data_value"));
  739. /* We are about to evaluate an expression in the context of the caller
  740. of the current frame. This evaluation context may be different from
  741. the current (callee's) context), so temporarily set the caller's context.
  742. It is possible for the caller to be from a different objfile from the
  743. callee if the call is made through a function pointer. */
  744. scoped_restore save_frame = make_scoped_restore (&this->m_frame,
  745. caller_frame);
  746. scoped_restore save_per_cu = make_scoped_restore (&this->m_per_cu,
  747. caller_per_cu);
  748. scoped_restore save_addr_info = make_scoped_restore (&this->m_addr_info,
  749. nullptr);
  750. scoped_restore save_per_objfile = make_scoped_restore (&this->m_per_objfile,
  751. caller_per_objfile);
  752. scoped_restore save_addr_size = make_scoped_restore (&this->m_addr_size);
  753. this->m_addr_size = this->m_per_cu->addr_size ();
  754. this->eval (data_src, size);
  755. }
  756. /* See expr.h. */
  757. value *
  758. dwarf_expr_context::fetch_result (struct type *type, struct type *subobj_type,
  759. LONGEST subobj_offset, bool as_lval)
  760. {
  761. value *retval = nullptr;
  762. gdbarch *arch = this->m_per_objfile->objfile->arch ();
  763. if (type == nullptr)
  764. type = address_type ();
  765. if (subobj_type == nullptr)
  766. subobj_type = type;
  767. if (this->m_pieces.size () > 0)
  768. {
  769. ULONGEST bit_size = 0;
  770. for (dwarf_expr_piece &piece : this->m_pieces)
  771. bit_size += piece.size;
  772. /* Complain if the expression is larger than the size of the
  773. outer type. */
  774. if (bit_size > 8 * TYPE_LENGTH (type))
  775. invalid_synthetic_pointer ();
  776. piece_closure *c
  777. = allocate_piece_closure (this->m_per_cu, this->m_per_objfile,
  778. std::move (this->m_pieces), this->m_frame);
  779. retval = allocate_computed_value (subobj_type,
  780. &pieced_value_funcs, c);
  781. set_value_offset (retval, subobj_offset);
  782. }
  783. else
  784. {
  785. /* If AS_LVAL is false, means that the implicit conversion
  786. from a location description to value is expected. */
  787. if (!as_lval)
  788. this->m_location = DWARF_VALUE_STACK;
  789. switch (this->m_location)
  790. {
  791. case DWARF_VALUE_REGISTER:
  792. {
  793. gdbarch *f_arch = get_frame_arch (this->m_frame);
  794. int dwarf_regnum
  795. = longest_to_int (value_as_long (this->fetch (0)));
  796. int gdb_regnum = dwarf_reg_to_regnum_or_error (f_arch,
  797. dwarf_regnum);
  798. if (subobj_offset != 0)
  799. error (_("cannot use offset on synthetic pointer to register"));
  800. gdb_assert (this->m_frame != NULL);
  801. retval = value_from_register (subobj_type, gdb_regnum,
  802. this->m_frame);
  803. if (value_optimized_out (retval))
  804. {
  805. /* This means the register has undefined value / was
  806. not saved. As we're computing the location of some
  807. variable etc. in the program, not a value for
  808. inspecting a register ($pc, $sp, etc.), return a
  809. generic optimized out value instead, so that we show
  810. <optimized out> instead of <not saved>. */
  811. value *tmp = allocate_value (subobj_type);
  812. value_contents_copy (tmp, 0, retval, 0,
  813. TYPE_LENGTH (subobj_type));
  814. retval = tmp;
  815. }
  816. }
  817. break;
  818. case DWARF_VALUE_MEMORY:
  819. {
  820. struct type *ptr_type;
  821. CORE_ADDR address = this->fetch_address (0);
  822. bool in_stack_memory = this->fetch_in_stack_memory (0);
  823. /* DW_OP_deref_size (and possibly other operations too) may
  824. create a pointer instead of an address. Ideally, the
  825. pointer to address conversion would be performed as part
  826. of those operations, but the type of the object to
  827. which the address refers is not known at the time of
  828. the operation. Therefore, we do the conversion here
  829. since the type is readily available. */
  830. switch (subobj_type->code ())
  831. {
  832. case TYPE_CODE_FUNC:
  833. case TYPE_CODE_METHOD:
  834. ptr_type = builtin_type (arch)->builtin_func_ptr;
  835. break;
  836. default:
  837. ptr_type = builtin_type (arch)->builtin_data_ptr;
  838. break;
  839. }
  840. address = value_as_address (value_from_pointer (ptr_type, address));
  841. retval = value_at_lazy (subobj_type,
  842. address + subobj_offset);
  843. if (in_stack_memory)
  844. set_value_stack (retval, 1);
  845. }
  846. break;
  847. case DWARF_VALUE_STACK:
  848. {
  849. value *val = this->fetch (0);
  850. size_t n = TYPE_LENGTH (value_type (val));
  851. size_t len = TYPE_LENGTH (subobj_type);
  852. size_t max = TYPE_LENGTH (type);
  853. if (subobj_offset + len > max)
  854. invalid_synthetic_pointer ();
  855. retval = allocate_value (subobj_type);
  856. /* The given offset is relative to the actual object. */
  857. if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG)
  858. subobj_offset += n - max;
  859. copy (value_contents_all (val).slice (subobj_offset, len),
  860. value_contents_raw (retval));
  861. }
  862. break;
  863. case DWARF_VALUE_LITERAL:
  864. {
  865. size_t n = TYPE_LENGTH (subobj_type);
  866. if (subobj_offset + n > this->m_len)
  867. invalid_synthetic_pointer ();
  868. retval = allocate_value (subobj_type);
  869. bfd_byte *contents = value_contents_raw (retval).data ();
  870. memcpy (contents, this->m_data + subobj_offset, n);
  871. }
  872. break;
  873. case DWARF_VALUE_OPTIMIZED_OUT:
  874. retval = allocate_optimized_out_value (subobj_type);
  875. break;
  876. /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
  877. operation by execute_stack_op. */
  878. case DWARF_VALUE_IMPLICIT_POINTER:
  879. /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
  880. it can only be encountered when making a piece. */
  881. default:
  882. internal_error (__FILE__, __LINE__, _("invalid location type"));
  883. }
  884. }
  885. set_value_initialized (retval, this->m_initialized);
  886. return retval;
  887. }
  888. /* See expr.h. */
  889. value *
  890. dwarf_expr_context::evaluate (const gdb_byte *addr, size_t len, bool as_lval,
  891. dwarf2_per_cu_data *per_cu, frame_info *frame,
  892. const struct property_addr_info *addr_info,
  893. struct type *type, struct type *subobj_type,
  894. LONGEST subobj_offset)
  895. {
  896. this->m_per_cu = per_cu;
  897. this->m_frame = frame;
  898. this->m_addr_info = addr_info;
  899. eval (addr, len);
  900. return fetch_result (type, subobj_type, subobj_offset, as_lval);
  901. }
  902. /* Require that TYPE be an integral type; throw an exception if not. */
  903. static void
  904. dwarf_require_integral (struct type *type)
  905. {
  906. if (type->code () != TYPE_CODE_INT
  907. && type->code () != TYPE_CODE_CHAR
  908. && type->code () != TYPE_CODE_BOOL)
  909. error (_("integral type expected in DWARF expression"));
  910. }
  911. /* Return the unsigned form of TYPE. TYPE is necessarily an integral
  912. type. */
  913. static struct type *
  914. get_unsigned_type (struct gdbarch *gdbarch, struct type *type)
  915. {
  916. switch (TYPE_LENGTH (type))
  917. {
  918. case 1:
  919. return builtin_type (gdbarch)->builtin_uint8;
  920. case 2:
  921. return builtin_type (gdbarch)->builtin_uint16;
  922. case 4:
  923. return builtin_type (gdbarch)->builtin_uint32;
  924. case 8:
  925. return builtin_type (gdbarch)->builtin_uint64;
  926. default:
  927. error (_("no unsigned variant found for type, while evaluating "
  928. "DWARF expression"));
  929. }
  930. }
  931. /* Return the signed form of TYPE. TYPE is necessarily an integral
  932. type. */
  933. static struct type *
  934. get_signed_type (struct gdbarch *gdbarch, struct type *type)
  935. {
  936. switch (TYPE_LENGTH (type))
  937. {
  938. case 1:
  939. return builtin_type (gdbarch)->builtin_int8;
  940. case 2:
  941. return builtin_type (gdbarch)->builtin_int16;
  942. case 4:
  943. return builtin_type (gdbarch)->builtin_int32;
  944. case 8:
  945. return builtin_type (gdbarch)->builtin_int64;
  946. default:
  947. error (_("no signed variant found for type, while evaluating "
  948. "DWARF expression"));
  949. }
  950. }
  951. /* Retrieve the N'th item on the stack, converted to an address. */
  952. CORE_ADDR
  953. dwarf_expr_context::fetch_address (int n)
  954. {
  955. gdbarch *arch = this->m_per_objfile->objfile->arch ();
  956. value *result_val = fetch (n);
  957. bfd_endian byte_order = gdbarch_byte_order (arch);
  958. ULONGEST result;
  959. dwarf_require_integral (value_type (result_val));
  960. result = extract_unsigned_integer (value_contents (result_val), byte_order);
  961. /* For most architectures, calling extract_unsigned_integer() alone
  962. is sufficient for extracting an address. However, some
  963. architectures (e.g. MIPS) use signed addresses and using
  964. extract_unsigned_integer() will not produce a correct
  965. result. Make sure we invoke gdbarch_integer_to_address()
  966. for those architectures which require it. */
  967. if (gdbarch_integer_to_address_p (arch))
  968. {
  969. gdb_byte *buf = (gdb_byte *) alloca (this->m_addr_size);
  970. type *int_type = get_unsigned_type (arch,
  971. value_type (result_val));
  972. store_unsigned_integer (buf, this->m_addr_size, byte_order, result);
  973. return gdbarch_integer_to_address (arch, int_type, buf);
  974. }
  975. return (CORE_ADDR) result;
  976. }
  977. /* Retrieve the in_stack_memory flag of the N'th item on the stack. */
  978. bool
  979. dwarf_expr_context::fetch_in_stack_memory (int n)
  980. {
  981. if (this->m_stack.size () <= n)
  982. error (_("Asked for position %d of stack, "
  983. "stack only has %zu elements on it."),
  984. n, this->m_stack.size ());
  985. return this->m_stack[this->m_stack.size () - (1 + n)].in_stack_memory;
  986. }
  987. /* Return true if the expression stack is empty. */
  988. bool
  989. dwarf_expr_context::stack_empty_p () const
  990. {
  991. return m_stack.empty ();
  992. }
  993. /* Add a new piece to the dwarf_expr_context's piece list. */
  994. void
  995. dwarf_expr_context::add_piece (ULONGEST size, ULONGEST offset)
  996. {
  997. this->m_pieces.emplace_back ();
  998. dwarf_expr_piece &p = this->m_pieces.back ();
  999. p.location = this->m_location;
  1000. p.size = size;
  1001. p.offset = offset;
  1002. if (p.location == DWARF_VALUE_LITERAL)
  1003. {
  1004. p.v.literal.data = this->m_data;
  1005. p.v.literal.length = this->m_len;
  1006. }
  1007. else if (stack_empty_p ())
  1008. {
  1009. p.location = DWARF_VALUE_OPTIMIZED_OUT;
  1010. /* Also reset the context's location, for our callers. This is
  1011. a somewhat strange approach, but this lets us avoid setting
  1012. the location to DWARF_VALUE_MEMORY in all the individual
  1013. cases in the evaluator. */
  1014. this->m_location = DWARF_VALUE_OPTIMIZED_OUT;
  1015. }
  1016. else if (p.location == DWARF_VALUE_MEMORY)
  1017. {
  1018. p.v.mem.addr = fetch_address (0);
  1019. p.v.mem.in_stack_memory = fetch_in_stack_memory (0);
  1020. }
  1021. else if (p.location == DWARF_VALUE_IMPLICIT_POINTER)
  1022. {
  1023. p.v.ptr.die_sect_off = (sect_offset) this->m_len;
  1024. p.v.ptr.offset = value_as_long (fetch (0));
  1025. }
  1026. else if (p.location == DWARF_VALUE_REGISTER)
  1027. p.v.regno = value_as_long (fetch (0));
  1028. else
  1029. {
  1030. p.v.value = fetch (0);
  1031. }
  1032. }
  1033. /* Evaluate the expression at ADDR (LEN bytes long). */
  1034. void
  1035. dwarf_expr_context::eval (const gdb_byte *addr, size_t len)
  1036. {
  1037. int old_recursion_depth = this->m_recursion_depth;
  1038. execute_stack_op (addr, addr + len);
  1039. /* RECURSION_DEPTH becomes invalid if an exception was thrown here. */
  1040. gdb_assert (this->m_recursion_depth == old_recursion_depth);
  1041. }
  1042. /* Helper to read a uleb128 value or throw an error. */
  1043. const gdb_byte *
  1044. safe_read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
  1045. uint64_t *r)
  1046. {
  1047. buf = gdb_read_uleb128 (buf, buf_end, r);
  1048. if (buf == NULL)
  1049. error (_("DWARF expression error: ran off end of buffer reading uleb128 value"));
  1050. return buf;
  1051. }
  1052. /* Helper to read a sleb128 value or throw an error. */
  1053. const gdb_byte *
  1054. safe_read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
  1055. int64_t *r)
  1056. {
  1057. buf = gdb_read_sleb128 (buf, buf_end, r);
  1058. if (buf == NULL)
  1059. error (_("DWARF expression error: ran off end of buffer reading sleb128 value"));
  1060. return buf;
  1061. }
  1062. const gdb_byte *
  1063. safe_skip_leb128 (const gdb_byte *buf, const gdb_byte *buf_end)
  1064. {
  1065. buf = gdb_skip_leb128 (buf, buf_end);
  1066. if (buf == NULL)
  1067. error (_("DWARF expression error: ran off end of buffer reading leb128 value"));
  1068. return buf;
  1069. }
  1070. /* Check that the current operator is either at the end of an
  1071. expression, or that it is followed by a composition operator or by
  1072. DW_OP_GNU_uninit (which should terminate the expression). */
  1073. void
  1074. dwarf_expr_require_composition (const gdb_byte *op_ptr, const gdb_byte *op_end,
  1075. const char *op_name)
  1076. {
  1077. if (op_ptr != op_end && *op_ptr != DW_OP_piece && *op_ptr != DW_OP_bit_piece
  1078. && *op_ptr != DW_OP_GNU_uninit)
  1079. error (_("DWARF-2 expression error: `%s' operations must be "
  1080. "used either alone or in conjunction with DW_OP_piece "
  1081. "or DW_OP_bit_piece."),
  1082. op_name);
  1083. }
  1084. /* Return true iff the types T1 and T2 are "the same". This only does
  1085. checks that might reasonably be needed to compare DWARF base
  1086. types. */
  1087. static int
  1088. base_types_equal_p (struct type *t1, struct type *t2)
  1089. {
  1090. if (t1->code () != t2->code ())
  1091. return 0;
  1092. if (t1->is_unsigned () != t2->is_unsigned ())
  1093. return 0;
  1094. return TYPE_LENGTH (t1) == TYPE_LENGTH (t2);
  1095. }
  1096. /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_reg* return the
  1097. DWARF register number. Otherwise return -1. */
  1098. int
  1099. dwarf_block_to_dwarf_reg (const gdb_byte *buf, const gdb_byte *buf_end)
  1100. {
  1101. uint64_t dwarf_reg;
  1102. if (buf_end <= buf)
  1103. return -1;
  1104. if (*buf >= DW_OP_reg0 && *buf <= DW_OP_reg31)
  1105. {
  1106. if (buf_end - buf != 1)
  1107. return -1;
  1108. return *buf - DW_OP_reg0;
  1109. }
  1110. if (*buf == DW_OP_regval_type || *buf == DW_OP_GNU_regval_type)
  1111. {
  1112. buf++;
  1113. buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
  1114. if (buf == NULL)
  1115. return -1;
  1116. buf = gdb_skip_leb128 (buf, buf_end);
  1117. if (buf == NULL)
  1118. return -1;
  1119. }
  1120. else if (*buf == DW_OP_regx)
  1121. {
  1122. buf++;
  1123. buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
  1124. if (buf == NULL)
  1125. return -1;
  1126. }
  1127. else
  1128. return -1;
  1129. if (buf != buf_end || (int) dwarf_reg != dwarf_reg)
  1130. return -1;
  1131. return dwarf_reg;
  1132. }
  1133. /* If <BUF..BUF_END] contains DW_FORM_block* with just DW_OP_breg*(0) and
  1134. DW_OP_deref* return the DWARF register number. Otherwise return -1.
  1135. DEREF_SIZE_RETURN contains -1 for DW_OP_deref; otherwise it contains the
  1136. size from DW_OP_deref_size. */
  1137. int
  1138. dwarf_block_to_dwarf_reg_deref (const gdb_byte *buf, const gdb_byte *buf_end,
  1139. CORE_ADDR *deref_size_return)
  1140. {
  1141. uint64_t dwarf_reg;
  1142. int64_t offset;
  1143. if (buf_end <= buf)
  1144. return -1;
  1145. if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
  1146. {
  1147. dwarf_reg = *buf - DW_OP_breg0;
  1148. buf++;
  1149. if (buf >= buf_end)
  1150. return -1;
  1151. }
  1152. else if (*buf == DW_OP_bregx)
  1153. {
  1154. buf++;
  1155. buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
  1156. if (buf == NULL)
  1157. return -1;
  1158. if ((int) dwarf_reg != dwarf_reg)
  1159. return -1;
  1160. }
  1161. else
  1162. return -1;
  1163. buf = gdb_read_sleb128 (buf, buf_end, &offset);
  1164. if (buf == NULL)
  1165. return -1;
  1166. if (offset != 0)
  1167. return -1;
  1168. if (*buf == DW_OP_deref)
  1169. {
  1170. buf++;
  1171. *deref_size_return = -1;
  1172. }
  1173. else if (*buf == DW_OP_deref_size)
  1174. {
  1175. buf++;
  1176. if (buf >= buf_end)
  1177. return -1;
  1178. *deref_size_return = *buf++;
  1179. }
  1180. else
  1181. return -1;
  1182. if (buf != buf_end)
  1183. return -1;
  1184. return dwarf_reg;
  1185. }
  1186. /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_fbreg(X) fill
  1187. in FB_OFFSET_RETURN with the X offset and return 1. Otherwise return 0. */
  1188. int
  1189. dwarf_block_to_fb_offset (const gdb_byte *buf, const gdb_byte *buf_end,
  1190. CORE_ADDR *fb_offset_return)
  1191. {
  1192. int64_t fb_offset;
  1193. if (buf_end <= buf)
  1194. return 0;
  1195. if (*buf != DW_OP_fbreg)
  1196. return 0;
  1197. buf++;
  1198. buf = gdb_read_sleb128 (buf, buf_end, &fb_offset);
  1199. if (buf == NULL)
  1200. return 0;
  1201. *fb_offset_return = fb_offset;
  1202. if (buf != buf_end || fb_offset != (LONGEST) *fb_offset_return)
  1203. return 0;
  1204. return 1;
  1205. }
  1206. /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_bregSP(X) fill
  1207. in SP_OFFSET_RETURN with the X offset and return 1. Otherwise return 0.
  1208. The matched SP register number depends on GDBARCH. */
  1209. int
  1210. dwarf_block_to_sp_offset (struct gdbarch *gdbarch, const gdb_byte *buf,
  1211. const gdb_byte *buf_end, CORE_ADDR *sp_offset_return)
  1212. {
  1213. uint64_t dwarf_reg;
  1214. int64_t sp_offset;
  1215. if (buf_end <= buf)
  1216. return 0;
  1217. if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
  1218. {
  1219. dwarf_reg = *buf - DW_OP_breg0;
  1220. buf++;
  1221. }
  1222. else
  1223. {
  1224. if (*buf != DW_OP_bregx)
  1225. return 0;
  1226. buf++;
  1227. buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
  1228. if (buf == NULL)
  1229. return 0;
  1230. }
  1231. if (dwarf_reg_to_regnum (gdbarch, dwarf_reg)
  1232. != gdbarch_sp_regnum (gdbarch))
  1233. return 0;
  1234. buf = gdb_read_sleb128 (buf, buf_end, &sp_offset);
  1235. if (buf == NULL)
  1236. return 0;
  1237. *sp_offset_return = sp_offset;
  1238. if (buf != buf_end || sp_offset != (LONGEST) *sp_offset_return)
  1239. return 0;
  1240. return 1;
  1241. }
  1242. /* The engine for the expression evaluator. Using the context in this
  1243. object, evaluate the expression between OP_PTR and OP_END. */
  1244. void
  1245. dwarf_expr_context::execute_stack_op (const gdb_byte *op_ptr,
  1246. const gdb_byte *op_end)
  1247. {
  1248. gdbarch *arch = this->m_per_objfile->objfile->arch ();
  1249. bfd_endian byte_order = gdbarch_byte_order (arch);
  1250. /* Old-style "untyped" DWARF values need special treatment in a
  1251. couple of places, specifically DW_OP_mod and DW_OP_shr. We need
  1252. a special type for these values so we can distinguish them from
  1253. values that have an explicit type, because explicitly-typed
  1254. values do not need special treatment. This special type must be
  1255. different (in the `==' sense) from any base type coming from the
  1256. CU. */
  1257. type *address_type = this->address_type ();
  1258. this->m_location = DWARF_VALUE_MEMORY;
  1259. this->m_initialized = 1; /* Default is initialized. */
  1260. if (this->m_recursion_depth > this->m_max_recursion_depth)
  1261. error (_("DWARF-2 expression error: Loop detected (%d)."),
  1262. this->m_recursion_depth);
  1263. this->m_recursion_depth++;
  1264. while (op_ptr < op_end)
  1265. {
  1266. dwarf_location_atom op = (dwarf_location_atom) *op_ptr++;
  1267. ULONGEST result;
  1268. /* Assume the value is not in stack memory.
  1269. Code that knows otherwise sets this to true.
  1270. Some arithmetic on stack addresses can probably be assumed to still
  1271. be a stack address, but we skip this complication for now.
  1272. This is just an optimization, so it's always ok to punt
  1273. and leave this as false. */
  1274. bool in_stack_memory = false;
  1275. uint64_t uoffset, reg;
  1276. int64_t offset;
  1277. value *result_val = NULL;
  1278. /* The DWARF expression might have a bug causing an infinite
  1279. loop. In that case, quitting is the only way out. */
  1280. QUIT;
  1281. switch (op)
  1282. {
  1283. case DW_OP_lit0:
  1284. case DW_OP_lit1:
  1285. case DW_OP_lit2:
  1286. case DW_OP_lit3:
  1287. case DW_OP_lit4:
  1288. case DW_OP_lit5:
  1289. case DW_OP_lit6:
  1290. case DW_OP_lit7:
  1291. case DW_OP_lit8:
  1292. case DW_OP_lit9:
  1293. case DW_OP_lit10:
  1294. case DW_OP_lit11:
  1295. case DW_OP_lit12:
  1296. case DW_OP_lit13:
  1297. case DW_OP_lit14:
  1298. case DW_OP_lit15:
  1299. case DW_OP_lit16:
  1300. case DW_OP_lit17:
  1301. case DW_OP_lit18:
  1302. case DW_OP_lit19:
  1303. case DW_OP_lit20:
  1304. case DW_OP_lit21:
  1305. case DW_OP_lit22:
  1306. case DW_OP_lit23:
  1307. case DW_OP_lit24:
  1308. case DW_OP_lit25:
  1309. case DW_OP_lit26:
  1310. case DW_OP_lit27:
  1311. case DW_OP_lit28:
  1312. case DW_OP_lit29:
  1313. case DW_OP_lit30:
  1314. case DW_OP_lit31:
  1315. result = op - DW_OP_lit0;
  1316. result_val = value_from_ulongest (address_type, result);
  1317. break;
  1318. case DW_OP_addr:
  1319. result = extract_unsigned_integer (op_ptr,
  1320. this->m_addr_size, byte_order);
  1321. op_ptr += this->m_addr_size;
  1322. /* Some versions of GCC emit DW_OP_addr before
  1323. DW_OP_GNU_push_tls_address. In this case the value is an
  1324. index, not an address. We don't support things like
  1325. branching between the address and the TLS op. */
  1326. if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
  1327. result += this->m_per_objfile->objfile->text_section_offset ();
  1328. result_val = value_from_ulongest (address_type, result);
  1329. break;
  1330. case DW_OP_addrx:
  1331. case DW_OP_GNU_addr_index:
  1332. ensure_have_per_cu (this->m_per_cu, "DW_OP_addrx");
  1333. op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
  1334. result = dwarf2_read_addr_index (this->m_per_cu, this->m_per_objfile,
  1335. uoffset);
  1336. result += this->m_per_objfile->objfile->text_section_offset ();
  1337. result_val = value_from_ulongest (address_type, result);
  1338. break;
  1339. case DW_OP_GNU_const_index:
  1340. ensure_have_per_cu (this->m_per_cu, "DW_OP_GNU_const_index");
  1341. op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
  1342. result = dwarf2_read_addr_index (this->m_per_cu, this->m_per_objfile,
  1343. uoffset);
  1344. result_val = value_from_ulongest (address_type, result);
  1345. break;
  1346. case DW_OP_const1u:
  1347. result = extract_unsigned_integer (op_ptr, 1, byte_order);
  1348. result_val = value_from_ulongest (address_type, result);
  1349. op_ptr += 1;
  1350. break;
  1351. case DW_OP_const1s:
  1352. result = extract_signed_integer (op_ptr, 1, byte_order);
  1353. result_val = value_from_ulongest (address_type, result);
  1354. op_ptr += 1;
  1355. break;
  1356. case DW_OP_const2u:
  1357. result = extract_unsigned_integer (op_ptr, 2, byte_order);
  1358. result_val = value_from_ulongest (address_type, result);
  1359. op_ptr += 2;
  1360. break;
  1361. case DW_OP_const2s:
  1362. result = extract_signed_integer (op_ptr, 2, byte_order);
  1363. result_val = value_from_ulongest (address_type, result);
  1364. op_ptr += 2;
  1365. break;
  1366. case DW_OP_const4u:
  1367. result = extract_unsigned_integer (op_ptr, 4, byte_order);
  1368. result_val = value_from_ulongest (address_type, result);
  1369. op_ptr += 4;
  1370. break;
  1371. case DW_OP_const4s:
  1372. result = extract_signed_integer (op_ptr, 4, byte_order);
  1373. result_val = value_from_ulongest (address_type, result);
  1374. op_ptr += 4;
  1375. break;
  1376. case DW_OP_const8u:
  1377. result = extract_unsigned_integer (op_ptr, 8, byte_order);
  1378. result_val = value_from_ulongest (address_type, result);
  1379. op_ptr += 8;
  1380. break;
  1381. case DW_OP_const8s:
  1382. result = extract_signed_integer (op_ptr, 8, byte_order);
  1383. result_val = value_from_ulongest (address_type, result);
  1384. op_ptr += 8;
  1385. break;
  1386. case DW_OP_constu:
  1387. op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
  1388. result = uoffset;
  1389. result_val = value_from_ulongest (address_type, result);
  1390. break;
  1391. case DW_OP_consts:
  1392. op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  1393. result = offset;
  1394. result_val = value_from_ulongest (address_type, result);
  1395. break;
  1396. /* The DW_OP_reg operations are required to occur alone in
  1397. location expressions. */
  1398. case DW_OP_reg0:
  1399. case DW_OP_reg1:
  1400. case DW_OP_reg2:
  1401. case DW_OP_reg3:
  1402. case DW_OP_reg4:
  1403. case DW_OP_reg5:
  1404. case DW_OP_reg6:
  1405. case DW_OP_reg7:
  1406. case DW_OP_reg8:
  1407. case DW_OP_reg9:
  1408. case DW_OP_reg10:
  1409. case DW_OP_reg11:
  1410. case DW_OP_reg12:
  1411. case DW_OP_reg13:
  1412. case DW_OP_reg14:
  1413. case DW_OP_reg15:
  1414. case DW_OP_reg16:
  1415. case DW_OP_reg17:
  1416. case DW_OP_reg18:
  1417. case DW_OP_reg19:
  1418. case DW_OP_reg20:
  1419. case DW_OP_reg21:
  1420. case DW_OP_reg22:
  1421. case DW_OP_reg23:
  1422. case DW_OP_reg24:
  1423. case DW_OP_reg25:
  1424. case DW_OP_reg26:
  1425. case DW_OP_reg27:
  1426. case DW_OP_reg28:
  1427. case DW_OP_reg29:
  1428. case DW_OP_reg30:
  1429. case DW_OP_reg31:
  1430. dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_reg");
  1431. result = op - DW_OP_reg0;
  1432. result_val = value_from_ulongest (address_type, result);
  1433. this->m_location = DWARF_VALUE_REGISTER;
  1434. break;
  1435. case DW_OP_regx:
  1436. op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
  1437. dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
  1438. result = reg;
  1439. result_val = value_from_ulongest (address_type, result);
  1440. this->m_location = DWARF_VALUE_REGISTER;
  1441. break;
  1442. case DW_OP_implicit_value:
  1443. {
  1444. uint64_t len;
  1445. op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
  1446. if (op_ptr + len > op_end)
  1447. error (_("DW_OP_implicit_value: too few bytes available."));
  1448. this->m_len = len;
  1449. this->m_data = op_ptr;
  1450. this->m_location = DWARF_VALUE_LITERAL;
  1451. op_ptr += len;
  1452. dwarf_expr_require_composition (op_ptr, op_end,
  1453. "DW_OP_implicit_value");
  1454. }
  1455. goto no_push;
  1456. case DW_OP_stack_value:
  1457. this->m_location = DWARF_VALUE_STACK;
  1458. dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
  1459. goto no_push;
  1460. case DW_OP_implicit_pointer:
  1461. case DW_OP_GNU_implicit_pointer:
  1462. {
  1463. int64_t len;
  1464. ensure_have_per_cu (this->m_per_cu, "DW_OP_implicit_pointer");
  1465. int ref_addr_size = this->m_per_cu->ref_addr_size ();
  1466. /* The referred-to DIE of sect_offset kind. */
  1467. this->m_len = extract_unsigned_integer (op_ptr, ref_addr_size,
  1468. byte_order);
  1469. op_ptr += ref_addr_size;
  1470. /* The byte offset into the data. */
  1471. op_ptr = safe_read_sleb128 (op_ptr, op_end, &len);
  1472. result = (ULONGEST) len;
  1473. result_val = value_from_ulongest (address_type, result);
  1474. this->m_location = DWARF_VALUE_IMPLICIT_POINTER;
  1475. dwarf_expr_require_composition (op_ptr, op_end,
  1476. "DW_OP_implicit_pointer");
  1477. }
  1478. break;
  1479. case DW_OP_breg0:
  1480. case DW_OP_breg1:
  1481. case DW_OP_breg2:
  1482. case DW_OP_breg3:
  1483. case DW_OP_breg4:
  1484. case DW_OP_breg5:
  1485. case DW_OP_breg6:
  1486. case DW_OP_breg7:
  1487. case DW_OP_breg8:
  1488. case DW_OP_breg9:
  1489. case DW_OP_breg10:
  1490. case DW_OP_breg11:
  1491. case DW_OP_breg12:
  1492. case DW_OP_breg13:
  1493. case DW_OP_breg14:
  1494. case DW_OP_breg15:
  1495. case DW_OP_breg16:
  1496. case DW_OP_breg17:
  1497. case DW_OP_breg18:
  1498. case DW_OP_breg19:
  1499. case DW_OP_breg20:
  1500. case DW_OP_breg21:
  1501. case DW_OP_breg22:
  1502. case DW_OP_breg23:
  1503. case DW_OP_breg24:
  1504. case DW_OP_breg25:
  1505. case DW_OP_breg26:
  1506. case DW_OP_breg27:
  1507. case DW_OP_breg28:
  1508. case DW_OP_breg29:
  1509. case DW_OP_breg30:
  1510. case DW_OP_breg31:
  1511. {
  1512. op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  1513. ensure_have_frame (this->m_frame, "DW_OP_breg");
  1514. result = read_addr_from_reg (this->m_frame, op - DW_OP_breg0);
  1515. result += offset;
  1516. result_val = value_from_ulongest (address_type, result);
  1517. }
  1518. break;
  1519. case DW_OP_bregx:
  1520. {
  1521. op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
  1522. op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  1523. ensure_have_frame (this->m_frame, "DW_OP_bregx");
  1524. result = read_addr_from_reg (this->m_frame, reg);
  1525. result += offset;
  1526. result_val = value_from_ulongest (address_type, result);
  1527. }
  1528. break;
  1529. case DW_OP_fbreg:
  1530. {
  1531. const gdb_byte *datastart;
  1532. size_t datalen;
  1533. op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  1534. /* Rather than create a whole new context, we simply
  1535. backup the current stack locally and install a new empty stack,
  1536. then reset it afterwards, effectively erasing whatever the
  1537. recursive call put there. */
  1538. std::vector<dwarf_stack_value> saved_stack = std::move (this->m_stack);
  1539. this->m_stack.clear ();
  1540. /* FIXME: cagney/2003-03-26: This code should be using
  1541. get_frame_base_address(), and then implement a dwarf2
  1542. specific this_base method. */
  1543. this->get_frame_base (&datastart, &datalen);
  1544. eval (datastart, datalen);
  1545. if (this->m_location == DWARF_VALUE_MEMORY)
  1546. result = fetch_address (0);
  1547. else if (this->m_location == DWARF_VALUE_REGISTER)
  1548. result
  1549. = read_addr_from_reg (this->m_frame, value_as_long (fetch (0)));
  1550. else
  1551. error (_("Not implemented: computing frame "
  1552. "base using explicit value operator"));
  1553. result = result + offset;
  1554. result_val = value_from_ulongest (address_type, result);
  1555. in_stack_memory = true;
  1556. /* Restore the content of the original stack. */
  1557. this->m_stack = std::move (saved_stack);
  1558. this->m_location = DWARF_VALUE_MEMORY;
  1559. }
  1560. break;
  1561. case DW_OP_dup:
  1562. result_val = fetch (0);
  1563. in_stack_memory = fetch_in_stack_memory (0);
  1564. break;
  1565. case DW_OP_drop:
  1566. pop ();
  1567. goto no_push;
  1568. case DW_OP_pick:
  1569. offset = *op_ptr++;
  1570. result_val = fetch (offset);
  1571. in_stack_memory = fetch_in_stack_memory (offset);
  1572. break;
  1573. case DW_OP_swap:
  1574. {
  1575. if (this->m_stack.size () < 2)
  1576. error (_("Not enough elements for "
  1577. "DW_OP_swap. Need 2, have %zu."),
  1578. this->m_stack.size ());
  1579. dwarf_stack_value &t1 = this->m_stack[this->m_stack.size () - 1];
  1580. dwarf_stack_value &t2 = this->m_stack[this->m_stack.size () - 2];
  1581. std::swap (t1, t2);
  1582. goto no_push;
  1583. }
  1584. case DW_OP_over:
  1585. result_val = fetch (1);
  1586. in_stack_memory = fetch_in_stack_memory (1);
  1587. break;
  1588. case DW_OP_rot:
  1589. {
  1590. if (this->m_stack.size () < 3)
  1591. error (_("Not enough elements for "
  1592. "DW_OP_rot. Need 3, have %zu."),
  1593. this->m_stack.size ());
  1594. dwarf_stack_value temp = this->m_stack[this->m_stack.size () - 1];
  1595. this->m_stack[this->m_stack.size () - 1]
  1596. = this->m_stack[this->m_stack.size () - 2];
  1597. this->m_stack[this->m_stack.size () - 2]
  1598. = this->m_stack[this->m_stack.size () - 3];
  1599. this->m_stack[this->m_stack.size () - 3] = temp;
  1600. goto no_push;
  1601. }
  1602. case DW_OP_deref:
  1603. case DW_OP_deref_size:
  1604. case DW_OP_deref_type:
  1605. case DW_OP_GNU_deref_type:
  1606. {
  1607. int addr_size = (op == DW_OP_deref ? this->m_addr_size : *op_ptr++);
  1608. gdb_byte *buf = (gdb_byte *) alloca (addr_size);
  1609. CORE_ADDR addr = fetch_address (0);
  1610. struct type *type;
  1611. pop ();
  1612. if (op == DW_OP_deref_type || op == DW_OP_GNU_deref_type)
  1613. {
  1614. op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
  1615. cu_offset type_die_cu_off = (cu_offset) uoffset;
  1616. type = get_base_type (type_die_cu_off);
  1617. }
  1618. else
  1619. type = address_type;
  1620. this->read_mem (buf, addr, addr_size);
  1621. /* If the size of the object read from memory is different
  1622. from the type length, we need to zero-extend it. */
  1623. if (TYPE_LENGTH (type) != addr_size)
  1624. {
  1625. ULONGEST datum =
  1626. extract_unsigned_integer (buf, addr_size, byte_order);
  1627. buf = (gdb_byte *) alloca (TYPE_LENGTH (type));
  1628. store_unsigned_integer (buf, TYPE_LENGTH (type),
  1629. byte_order, datum);
  1630. }
  1631. result_val = value_from_contents_and_address (type, buf, addr);
  1632. break;
  1633. }
  1634. case DW_OP_abs:
  1635. case DW_OP_neg:
  1636. case DW_OP_not:
  1637. case DW_OP_plus_uconst:
  1638. {
  1639. /* Unary operations. */
  1640. result_val = fetch (0);
  1641. pop ();
  1642. switch (op)
  1643. {
  1644. case DW_OP_abs:
  1645. if (value_less (result_val,
  1646. value_zero (value_type (result_val), not_lval)))
  1647. result_val = value_neg (result_val);
  1648. break;
  1649. case DW_OP_neg:
  1650. result_val = value_neg (result_val);
  1651. break;
  1652. case DW_OP_not:
  1653. dwarf_require_integral (value_type (result_val));
  1654. result_val = value_complement (result_val);
  1655. break;
  1656. case DW_OP_plus_uconst:
  1657. dwarf_require_integral (value_type (result_val));
  1658. result = value_as_long (result_val);
  1659. op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
  1660. result += reg;
  1661. result_val = value_from_ulongest (address_type, result);
  1662. break;
  1663. }
  1664. }
  1665. break;
  1666. case DW_OP_and:
  1667. case DW_OP_div:
  1668. case DW_OP_minus:
  1669. case DW_OP_mod:
  1670. case DW_OP_mul:
  1671. case DW_OP_or:
  1672. case DW_OP_plus:
  1673. case DW_OP_shl:
  1674. case DW_OP_shr:
  1675. case DW_OP_shra:
  1676. case DW_OP_xor:
  1677. case DW_OP_le:
  1678. case DW_OP_ge:
  1679. case DW_OP_eq:
  1680. case DW_OP_lt:
  1681. case DW_OP_gt:
  1682. case DW_OP_ne:
  1683. {
  1684. /* Binary operations. */
  1685. struct value *first, *second;
  1686. second = fetch (0);
  1687. pop ();
  1688. first = fetch (0);
  1689. pop ();
  1690. if (! base_types_equal_p (value_type (first), value_type (second)))
  1691. error (_("Incompatible types on DWARF stack"));
  1692. switch (op)
  1693. {
  1694. case DW_OP_and:
  1695. dwarf_require_integral (value_type (first));
  1696. dwarf_require_integral (value_type (second));
  1697. result_val = value_binop (first, second, BINOP_BITWISE_AND);
  1698. break;
  1699. case DW_OP_div:
  1700. result_val = value_binop (first, second, BINOP_DIV);
  1701. break;
  1702. case DW_OP_minus:
  1703. result_val = value_binop (first, second, BINOP_SUB);
  1704. break;
  1705. case DW_OP_mod:
  1706. {
  1707. int cast_back = 0;
  1708. struct type *orig_type = value_type (first);
  1709. /* We have to special-case "old-style" untyped values
  1710. -- these must have mod computed using unsigned
  1711. math. */
  1712. if (orig_type == address_type)
  1713. {
  1714. struct type *utype = get_unsigned_type (arch, orig_type);
  1715. cast_back = 1;
  1716. first = value_cast (utype, first);
  1717. second = value_cast (utype, second);
  1718. }
  1719. /* Note that value_binop doesn't handle float or
  1720. decimal float here. This seems unimportant. */
  1721. result_val = value_binop (first, second, BINOP_MOD);
  1722. if (cast_back)
  1723. result_val = value_cast (orig_type, result_val);
  1724. }
  1725. break;
  1726. case DW_OP_mul:
  1727. result_val = value_binop (first, second, BINOP_MUL);
  1728. break;
  1729. case DW_OP_or:
  1730. dwarf_require_integral (value_type (first));
  1731. dwarf_require_integral (value_type (second));
  1732. result_val = value_binop (first, second, BINOP_BITWISE_IOR);
  1733. break;
  1734. case DW_OP_plus:
  1735. result_val = value_binop (first, second, BINOP_ADD);
  1736. break;
  1737. case DW_OP_shl:
  1738. dwarf_require_integral (value_type (first));
  1739. dwarf_require_integral (value_type (second));
  1740. result_val = value_binop (first, second, BINOP_LSH);
  1741. break;
  1742. case DW_OP_shr:
  1743. dwarf_require_integral (value_type (first));
  1744. dwarf_require_integral (value_type (second));
  1745. if (!value_type (first)->is_unsigned ())
  1746. {
  1747. struct type *utype
  1748. = get_unsigned_type (arch, value_type (first));
  1749. first = value_cast (utype, first);
  1750. }
  1751. result_val = value_binop (first, second, BINOP_RSH);
  1752. /* Make sure we wind up with the same type we started
  1753. with. */
  1754. if (value_type (result_val) != value_type (second))
  1755. result_val = value_cast (value_type (second), result_val);
  1756. break;
  1757. case DW_OP_shra:
  1758. dwarf_require_integral (value_type (first));
  1759. dwarf_require_integral (value_type (second));
  1760. if (value_type (first)->is_unsigned ())
  1761. {
  1762. struct type *stype
  1763. = get_signed_type (arch, value_type (first));
  1764. first = value_cast (stype, first);
  1765. }
  1766. result_val = value_binop (first, second, BINOP_RSH);
  1767. /* Make sure we wind up with the same type we started
  1768. with. */
  1769. if (value_type (result_val) != value_type (second))
  1770. result_val = value_cast (value_type (second), result_val);
  1771. break;
  1772. case DW_OP_xor:
  1773. dwarf_require_integral (value_type (first));
  1774. dwarf_require_integral (value_type (second));
  1775. result_val = value_binop (first, second, BINOP_BITWISE_XOR);
  1776. break;
  1777. case DW_OP_le:
  1778. /* A <= B is !(B < A). */
  1779. result = ! value_less (second, first);
  1780. result_val = value_from_ulongest (address_type, result);
  1781. break;
  1782. case DW_OP_ge:
  1783. /* A >= B is !(A < B). */
  1784. result = ! value_less (first, second);
  1785. result_val = value_from_ulongest (address_type, result);
  1786. break;
  1787. case DW_OP_eq:
  1788. result = value_equal (first, second);
  1789. result_val = value_from_ulongest (address_type, result);
  1790. break;
  1791. case DW_OP_lt:
  1792. result = value_less (first, second);
  1793. result_val = value_from_ulongest (address_type, result);
  1794. break;
  1795. case DW_OP_gt:
  1796. /* A > B is B < A. */
  1797. result = value_less (second, first);
  1798. result_val = value_from_ulongest (address_type, result);
  1799. break;
  1800. case DW_OP_ne:
  1801. result = ! value_equal (first, second);
  1802. result_val = value_from_ulongest (address_type, result);
  1803. break;
  1804. default:
  1805. internal_error (__FILE__, __LINE__,
  1806. _("Can't be reached."));
  1807. }
  1808. }
  1809. break;
  1810. case DW_OP_call_frame_cfa:
  1811. ensure_have_frame (this->m_frame, "DW_OP_call_frame_cfa");
  1812. result = dwarf2_frame_cfa (this->m_frame);
  1813. result_val = value_from_ulongest (address_type, result);
  1814. in_stack_memory = true;
  1815. break;
  1816. case DW_OP_GNU_push_tls_address:
  1817. case DW_OP_form_tls_address:
  1818. /* Variable is at a constant offset in the thread-local
  1819. storage block into the objfile for the current thread and
  1820. the dynamic linker module containing this expression. Here
  1821. we return returns the offset from that base. The top of the
  1822. stack has the offset from the beginning of the thread
  1823. control block at which the variable is located. Nothing
  1824. should follow this operator, so the top of stack would be
  1825. returned. */
  1826. result = value_as_long (fetch (0));
  1827. pop ();
  1828. result = target_translate_tls_address (this->m_per_objfile->objfile,
  1829. result);
  1830. result_val = value_from_ulongest (address_type, result);
  1831. break;
  1832. case DW_OP_skip:
  1833. offset = extract_signed_integer (op_ptr, 2, byte_order);
  1834. op_ptr += 2;
  1835. op_ptr += offset;
  1836. goto no_push;
  1837. case DW_OP_bra:
  1838. {
  1839. struct value *val;
  1840. offset = extract_signed_integer (op_ptr, 2, byte_order);
  1841. op_ptr += 2;
  1842. val = fetch (0);
  1843. dwarf_require_integral (value_type (val));
  1844. if (value_as_long (val) != 0)
  1845. op_ptr += offset;
  1846. pop ();
  1847. }
  1848. goto no_push;
  1849. case DW_OP_nop:
  1850. goto no_push;
  1851. case DW_OP_piece:
  1852. {
  1853. uint64_t size;
  1854. /* Record the piece. */
  1855. op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
  1856. add_piece (8 * size, 0);
  1857. /* Pop off the address/regnum, and reset the location
  1858. type. */
  1859. if (this->m_location != DWARF_VALUE_LITERAL
  1860. && this->m_location != DWARF_VALUE_OPTIMIZED_OUT)
  1861. pop ();
  1862. this->m_location = DWARF_VALUE_MEMORY;
  1863. }
  1864. goto no_push;
  1865. case DW_OP_bit_piece:
  1866. {
  1867. uint64_t size, uleb_offset;
  1868. /* Record the piece. */
  1869. op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
  1870. op_ptr = safe_read_uleb128 (op_ptr, op_end, &uleb_offset);
  1871. add_piece (size, uleb_offset);
  1872. /* Pop off the address/regnum, and reset the location
  1873. type. */
  1874. if (this->m_location != DWARF_VALUE_LITERAL
  1875. && this->m_location != DWARF_VALUE_OPTIMIZED_OUT)
  1876. pop ();
  1877. this->m_location = DWARF_VALUE_MEMORY;
  1878. }
  1879. goto no_push;
  1880. case DW_OP_GNU_uninit:
  1881. if (op_ptr != op_end)
  1882. error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
  1883. "be the very last op."));
  1884. this->m_initialized = 0;
  1885. goto no_push;
  1886. case DW_OP_call2:
  1887. {
  1888. cu_offset cu_off
  1889. = (cu_offset) extract_unsigned_integer (op_ptr, 2, byte_order);
  1890. op_ptr += 2;
  1891. this->dwarf_call (cu_off);
  1892. }
  1893. goto no_push;
  1894. case DW_OP_call4:
  1895. {
  1896. cu_offset cu_off
  1897. = (cu_offset) extract_unsigned_integer (op_ptr, 4, byte_order);
  1898. op_ptr += 4;
  1899. this->dwarf_call (cu_off);
  1900. }
  1901. goto no_push;
  1902. case DW_OP_GNU_variable_value:
  1903. {
  1904. ensure_have_per_cu (this->m_per_cu, "DW_OP_GNU_variable_value");
  1905. int ref_addr_size = this->m_per_cu->ref_addr_size ();
  1906. sect_offset sect_off
  1907. = (sect_offset) extract_unsigned_integer (op_ptr,
  1908. ref_addr_size,
  1909. byte_order);
  1910. op_ptr += ref_addr_size;
  1911. result_val = sect_variable_value (sect_off, this->m_per_cu,
  1912. this->m_per_objfile);
  1913. result_val = value_cast (address_type, result_val);
  1914. }
  1915. break;
  1916. case DW_OP_entry_value:
  1917. case DW_OP_GNU_entry_value:
  1918. {
  1919. uint64_t len;
  1920. CORE_ADDR deref_size;
  1921. union call_site_parameter_u kind_u;
  1922. op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
  1923. if (op_ptr + len > op_end)
  1924. error (_("DW_OP_entry_value: too few bytes available."));
  1925. kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (op_ptr, op_ptr + len);
  1926. if (kind_u.dwarf_reg != -1)
  1927. {
  1928. op_ptr += len;
  1929. this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG,
  1930. kind_u,
  1931. -1 /* deref_size */);
  1932. goto no_push;
  1933. }
  1934. kind_u.dwarf_reg = dwarf_block_to_dwarf_reg_deref (op_ptr,
  1935. op_ptr + len,
  1936. &deref_size);
  1937. if (kind_u.dwarf_reg != -1)
  1938. {
  1939. if (deref_size == -1)
  1940. deref_size = this->m_addr_size;
  1941. op_ptr += len;
  1942. this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG,
  1943. kind_u, deref_size);
  1944. goto no_push;
  1945. }
  1946. error (_("DWARF-2 expression error: DW_OP_entry_value is "
  1947. "supported only for single DW_OP_reg* "
  1948. "or for DW_OP_breg*(0)+DW_OP_deref*"));
  1949. }
  1950. case DW_OP_GNU_parameter_ref:
  1951. {
  1952. union call_site_parameter_u kind_u;
  1953. kind_u.param_cu_off
  1954. = (cu_offset) extract_unsigned_integer (op_ptr, 4, byte_order);
  1955. op_ptr += 4;
  1956. this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_PARAM_OFFSET,
  1957. kind_u,
  1958. -1 /* deref_size */);
  1959. }
  1960. goto no_push;
  1961. case DW_OP_const_type:
  1962. case DW_OP_GNU_const_type:
  1963. {
  1964. int n;
  1965. const gdb_byte *data;
  1966. struct type *type;
  1967. op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
  1968. cu_offset type_die_cu_off = (cu_offset) uoffset;
  1969. n = *op_ptr++;
  1970. data = op_ptr;
  1971. op_ptr += n;
  1972. type = get_base_type (type_die_cu_off);
  1973. if (TYPE_LENGTH (type) != n)
  1974. error (_("DW_OP_const_type has different sizes for type and data"));
  1975. result_val = value_from_contents (type, data);
  1976. }
  1977. break;
  1978. case DW_OP_regval_type:
  1979. case DW_OP_GNU_regval_type:
  1980. {
  1981. op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
  1982. op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
  1983. cu_offset type_die_cu_off = (cu_offset) uoffset;
  1984. ensure_have_frame (this->m_frame, "DW_OP_regval_type");
  1985. struct type *type = get_base_type (type_die_cu_off);
  1986. int regnum
  1987. = dwarf_reg_to_regnum_or_error (get_frame_arch (this->m_frame),
  1988. reg);
  1989. result_val = value_from_register (type, regnum, this->m_frame);
  1990. }
  1991. break;
  1992. case DW_OP_convert:
  1993. case DW_OP_GNU_convert:
  1994. case DW_OP_reinterpret:
  1995. case DW_OP_GNU_reinterpret:
  1996. {
  1997. struct type *type;
  1998. op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
  1999. cu_offset type_die_cu_off = (cu_offset) uoffset;
  2000. if (to_underlying (type_die_cu_off) == 0)
  2001. type = address_type;
  2002. else
  2003. type = get_base_type (type_die_cu_off);
  2004. result_val = fetch (0);
  2005. pop ();
  2006. if (op == DW_OP_convert || op == DW_OP_GNU_convert)
  2007. result_val = value_cast (type, result_val);
  2008. else if (type == value_type (result_val))
  2009. {
  2010. /* Nothing. */
  2011. }
  2012. else if (TYPE_LENGTH (type)
  2013. != TYPE_LENGTH (value_type (result_val)))
  2014. error (_("DW_OP_reinterpret has wrong size"));
  2015. else
  2016. result_val
  2017. = value_from_contents (type,
  2018. value_contents_all (result_val).data ());
  2019. }
  2020. break;
  2021. case DW_OP_push_object_address:
  2022. /* Return the address of the object we are currently observing. */
  2023. if (this->m_addr_info == nullptr
  2024. || (this->m_addr_info->valaddr.data () == nullptr
  2025. && this->m_addr_info->addr == 0))
  2026. error (_("Location address is not set."));
  2027. result_val
  2028. = value_from_ulongest (address_type, this->m_addr_info->addr);
  2029. break;
  2030. default:
  2031. error (_("Unhandled dwarf expression opcode 0x%x"), op);
  2032. }
  2033. /* Most things push a result value. */
  2034. gdb_assert (result_val != NULL);
  2035. push (result_val, in_stack_memory);
  2036. no_push:
  2037. ;
  2038. }
  2039. /* To simplify our main caller, if the result is an implicit
  2040. pointer, then make a pieced value. This is ok because we can't
  2041. have implicit pointers in contexts where pieces are invalid. */
  2042. if (this->m_location == DWARF_VALUE_IMPLICIT_POINTER)
  2043. add_piece (8 * this->m_addr_size, 0);
  2044. this->m_recursion_depth--;
  2045. gdb_assert (this->m_recursion_depth >= 0);
  2046. }
  2047. void _initialize_dwarf2expr ();
  2048. void
  2049. _initialize_dwarf2expr ()
  2050. {
  2051. dwarf_arch_cookie
  2052. = gdbarch_data_register_post_init (dwarf_gdbarch_types_init);
  2053. }