@@ -121,6 +121,7 @@ struct compilation_unit {
121121 uint64_t debug_abbrev_offset ;
122122 uint8_t address_size ;
123123 bool is_64_bit ;
124+ bool bswap ;
124125};
125126
126127static inline const char * section_ptr (Elf_Data * data , size_t offset )
@@ -302,10 +303,6 @@ static struct drgn_error *read_sections(struct drgn_dwarf_index_file *file)
302303 if (!ehdr )
303304 return & drgn_not_elf ;
304305
305- file -> bswap = (ehdr -> e_ident [EI_DATA ] !=
306- (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ ?
307- ELFDATA2LSB : ELFDATA2MSB ));
308-
309306 if (elf_getshdrstrndx (file -> elf , & shstrndx ))
310307 return drgn_error_libelf ();
311308
@@ -650,17 +647,17 @@ static struct drgn_error *read_compilation_unit_header(const char *ptr,
650647 uint32_t tmp ;
651648 uint16_t version ;
652649
653- if (!read_u32 (& ptr , end , cu -> file -> bswap , & tmp ))
650+ if (!read_u32 (& ptr , end , cu -> bswap , & tmp ))
654651 return drgn_eof ();
655652 cu -> is_64_bit = tmp == UINT32_C (0xffffffff );
656653 if (cu -> is_64_bit ) {
657- if (!read_u64 (& ptr , end , cu -> file -> bswap , & cu -> unit_length ))
654+ if (!read_u64 (& ptr , end , cu -> bswap , & cu -> unit_length ))
658655 return drgn_eof ();
659656 } else {
660657 cu -> unit_length = tmp ;
661658 }
662659
663- if (!read_u16 (& ptr , end , cu -> file -> bswap , & version ))
660+ if (!read_u16 (& ptr , end , cu -> bswap , & version ))
664661 return drgn_eof ();
665662 if (version != 2 && version != 3 && version != 4 ) {
666663 return drgn_error_format (DRGN_ERROR_DWARF_FORMAT ,
@@ -669,11 +666,10 @@ static struct drgn_error *read_compilation_unit_header(const char *ptr,
669666 }
670667
671668 if (cu -> is_64_bit ) {
672- if (!read_u64 (& ptr , end , cu -> file -> bswap ,
673- & cu -> debug_abbrev_offset ))
669+ if (!read_u64 (& ptr , end , cu -> bswap , & cu -> debug_abbrev_offset ))
674670 return drgn_eof ();
675671 } else {
676- if (!read_u32_into_u64 (& ptr , end , cu -> file -> bswap ,
672+ if (!read_u32_into_u64 (& ptr , end , cu -> bswap ,
677673 & cu -> debug_abbrev_offset ))
678674 return drgn_eof ();
679675 }
@@ -689,10 +685,15 @@ static struct drgn_error *read_cus(struct drgn_dwarf_index_file *file,
689685 size_t * num_cus , size_t * cus_capacity )
690686{
691687 struct drgn_error * err ;
688+ bool bswap ;
692689 Elf_Data * debug_info = file -> sections [SECTION_DEBUG_INFO ];
693690 const char * ptr = section_ptr (debug_info , 0 );
694691 const char * end = section_end (debug_info );
695692
693+ bswap = (elf_getident (file -> elf , NULL )[EI_DATA ] !=
694+ (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ ?
695+ ELFDATA2LSB : ELFDATA2MSB ));
696+
696697 while (ptr < end ) {
697698 struct compilation_unit * cu ;
698699
@@ -711,6 +712,7 @@ static struct drgn_error *read_cus(struct drgn_dwarf_index_file *file,
711712 cu = & (* cus )[(* num_cus )++ ];
712713 cu -> file = file ;
713714 cu -> ptr = ptr ;
715+ cu -> bswap = bswap ;
714716 if ((err = read_compilation_unit_header (ptr , end , cu )))
715717 return err ;
716718
@@ -1008,21 +1010,21 @@ static struct drgn_error *read_abbrev_table(const char *ptr, const char *end,
10081010 return NULL ;
10091011}
10101012
1011- static struct drgn_error * skip_lnp_header (struct drgn_dwarf_index_file * file ,
1013+ static struct drgn_error * skip_lnp_header (struct compilation_unit * cu ,
10121014 const char * * ptr , const char * end )
10131015{
10141016 uint32_t tmp ;
10151017 bool is_64_bit ;
10161018 uint16_t version ;
10171019 uint8_t opcode_base ;
10181020
1019- if (!read_u32 (ptr , end , file -> bswap , & tmp ))
1021+ if (!read_u32 (ptr , end , cu -> bswap , & tmp ))
10201022 return drgn_eof ();
10211023 is_64_bit = tmp == UINT32_C (0xffffffff );
10221024 if (is_64_bit )
10231025 * ptr += sizeof (uint64_t );
10241026
1025- if (!read_u16 (ptr , end , file -> bswap , & version ))
1027+ if (!read_u16 (ptr , end , cu -> bswap , & version ))
10261028 return drgn_eof ();
10271029 if (version != 2 && version != 3 && version != 4 ) {
10281030 return drgn_error_format (DRGN_ERROR_DWARF_FORMAT ,
@@ -1091,7 +1093,8 @@ read_file_name_table(struct drgn_dwarf_index *dindex,
10911093
10921094 siphash_vector_init (& directories );
10931095
1094- if ((err = skip_lnp_header (file , & ptr , end )))
1096+ err = skip_lnp_header (cu , & ptr , end );
1097+ if (err )
10951098 return err ;
10961099
10971100 for (;;) {
@@ -1285,13 +1288,11 @@ static struct drgn_error *read_die(struct compilation_unit *cu,
12851288 return drgn_eof ();
12861289 goto skip ;
12871290 case ATTRIB_BLOCK2 :
1288- if (!read_u16_into_size_t (ptr , end , cu -> file -> bswap ,
1289- & skip ))
1291+ if (!read_u16_into_size_t (ptr , end , cu -> bswap , & skip ))
12901292 return drgn_eof ();
12911293 goto skip ;
12921294 case ATTRIB_BLOCK4 :
1293- if (!read_u32_into_size_t (ptr , end , cu -> file -> bswap ,
1294- & skip ))
1295+ if (!read_u32_into_size_t (ptr , end , cu -> bswap , & skip ))
12951296 return drgn_eof ();
12961297 goto skip ;
12971298 case ATTRIB_EXPRLOC :
@@ -1314,18 +1315,15 @@ static struct drgn_error *read_die(struct compilation_unit *cu,
13141315 return drgn_eof ();
13151316 goto sibling ;
13161317 case ATTRIB_SIBLING_REF2 :
1317- if (!read_u16_into_size_t (ptr , end , cu -> file -> bswap ,
1318- & tmp ))
1318+ if (!read_u16_into_size_t (ptr , end , cu -> bswap , & tmp ))
13191319 return drgn_eof ();
13201320 goto sibling ;
13211321 case ATTRIB_SIBLING_REF4 :
1322- if (!read_u32_into_size_t (ptr , end , cu -> file -> bswap ,
1323- & tmp ))
1322+ if (!read_u32_into_size_t (ptr , end , cu -> bswap , & tmp ))
13241323 return drgn_eof ();
13251324 goto sibling ;
13261325 case ATTRIB_SIBLING_REF8 :
1327- if (!read_u64_into_size_t (ptr , end , cu -> file -> bswap ,
1328- & tmp ))
1326+ if (!read_u64_into_size_t (ptr , end , cu -> bswap , & tmp ))
13291327 return drgn_eof ();
13301328 goto sibling ;
13311329 case ATTRIB_SIBLING_REF_UDATA :
@@ -1338,13 +1336,11 @@ static struct drgn_error *read_die(struct compilation_unit *cu,
13381336 __builtin_prefetch (die -> sibling );
13391337 break ;
13401338 case ATTRIB_NAME_STRP4 :
1341- if (!read_u32_into_size_t (ptr , end , cu -> file -> bswap ,
1342- & tmp ))
1339+ if (!read_u32_into_size_t (ptr , end , cu -> bswap , & tmp ))
13431340 return drgn_eof ();
13441341 goto strp ;
13451342 case ATTRIB_NAME_STRP8 :
1346- if (!read_u64_into_size_t (ptr , end , cu -> file -> bswap ,
1347- & tmp ))
1343+ if (!read_u64_into_size_t (ptr , end , cu -> bswap , & tmp ))
13481344 return drgn_eof ();
13491345strp :
13501346 if (!read_in_bounds (debug_str_buffer , debug_str_end ,
@@ -1354,12 +1350,12 @@ static struct drgn_error *read_die(struct compilation_unit *cu,
13541350 __builtin_prefetch (die -> name );
13551351 break ;
13561352 case ATTRIB_STMT_LIST_LINEPTR4 :
1357- if (!read_u32_into_size_t (ptr , end , cu -> file -> bswap ,
1353+ if (!read_u32_into_size_t (ptr , end , cu -> bswap ,
13581354 & die -> stmt_list ))
13591355 return drgn_eof ();
13601356 break ;
13611357 case ATTRIB_STMT_LIST_LINEPTR8 :
1362- if (!read_u64_into_size_t (ptr , end , cu -> file -> bswap ,
1358+ if (!read_u64_into_size_t (ptr , end , cu -> bswap ,
13631359 & die -> stmt_list ))
13641360 return drgn_eof ();
13651361 break ;
@@ -1368,17 +1364,17 @@ static struct drgn_error *read_die(struct compilation_unit *cu,
13681364 return drgn_eof ();
13691365 break ;
13701366 case ATTRIB_DECL_FILE_DATA2 :
1371- if (!read_u16_into_size_t (ptr , end , cu -> file -> bswap ,
1367+ if (!read_u16_into_size_t (ptr , end , cu -> bswap ,
13721368 & die -> decl_file ))
13731369 return drgn_eof ();
13741370 break ;
13751371 case ATTRIB_DECL_FILE_DATA4 :
1376- if (!read_u32_into_size_t (ptr , end , cu -> file -> bswap ,
1372+ if (!read_u32_into_size_t (ptr , end , cu -> bswap ,
13771373 & die -> decl_file ))
13781374 return drgn_eof ();
13791375 break ;
13801376 case ATTRIB_DECL_FILE_DATA8 :
1381- if (!read_u64_into_size_t (ptr , end , cu -> file -> bswap ,
1377+ if (!read_u64_into_size_t (ptr , end , cu -> bswap ,
13821378 & die -> decl_file ))
13831379 return drgn_eof ();
13841380 break ;
@@ -1392,18 +1388,15 @@ static struct drgn_error *read_die(struct compilation_unit *cu,
13921388 return drgn_eof ();
13931389 goto specification ;
13941390 case ATTRIB_SPECIFICATION_REF2 :
1395- if (!read_u16_into_size_t (ptr , end , cu -> file -> bswap ,
1396- & tmp ))
1391+ if (!read_u16_into_size_t (ptr , end , cu -> bswap , & tmp ))
13971392 return drgn_eof ();
13981393 goto specification ;
13991394 case ATTRIB_SPECIFICATION_REF4 :
1400- if (!read_u32_into_size_t (ptr , end , cu -> file -> bswap ,
1401- & tmp ))
1395+ if (!read_u32_into_size_t (ptr , end , cu -> bswap , & tmp ))
14021396 return drgn_eof ();
14031397 goto specification ;
14041398 case ATTRIB_SPECIFICATION_REF8 :
1405- if (!read_u64_into_size_t (ptr , end , cu -> file -> bswap ,
1406- & tmp ))
1399+ if (!read_u64_into_size_t (ptr , end , cu -> bswap , & tmp ))
14071400 return drgn_eof ();
14081401 goto specification ;
14091402 case ATTRIB_SPECIFICATION_REF_UDATA :
0 commit comments