@@ -2734,14 +2734,99 @@ static uint32_t getDebugNamesHeaderSize(uint32_t augmentationStringSize) {
2734
2734
/* Augmentation string */ augmentationStringSize;
2735
2735
}
2736
2736
2737
+ static Expected<DebugNamesBaseSection::IndexEntry *>
2738
+ readEntry (uint64_t &offset, const DWARFDebugNames::NameIndex &ni,
2739
+ uint64_t entriesBase, DWARFDataExtractor &namesExtractor,
2740
+ const LLDDWARFSection &namesSec) {
2741
+ std::string errMsg;
2742
+ auto ie = makeThreadLocal<DebugNamesBaseSection::IndexEntry>();
2743
+ ie->poolOffset = offset;
2744
+ Error err = Error::success ();
2745
+ uint64_t ulebVal = namesExtractor.getULEB128 (&offset, &err);
2746
+ if (err) {
2747
+ errMsg = " : invalid abbrev code in entry: " ;
2748
+ errMsg.append (toString (std::move (err)));
2749
+ return createStringError (inconvertibleErrorCode (), errMsg.c_str ());
2750
+ }
2751
+ if (ulebVal < UINT32_MAX)
2752
+ ie->abbrevCode = static_cast <uint32_t >(ulebVal);
2753
+ else {
2754
+ errMsg = " : abbrev code in entry too large for DWARF32: " ;
2755
+ errMsg.append (std::to_string (ulebVal));
2756
+ return createStringError (inconvertibleErrorCode (), errMsg.c_str ());
2757
+ }
2758
+ auto it = ni.getAbbrevs ().find_as (ie->abbrevCode );
2759
+ if (it == ni.getAbbrevs ().end ()) {
2760
+ errMsg = " : entry abbrev code not found in abbrev table: " ;
2761
+ errMsg.append (std::to_string (ie->abbrevCode ));
2762
+ return createStringError (inconvertibleErrorCode (), errMsg.c_str ());
2763
+ }
2764
+
2765
+ DebugNamesBaseSection::AttrValue attr, cuAttr = {0 , 0 };
2766
+ for (DWARFDebugNames::AttributeEncoding a : it->Attributes ) {
2767
+ if (a.Index == dwarf::DW_IDX_parent) {
2768
+ if (a.Form == dwarf::DW_FORM_ref4) {
2769
+ attr.attrValue = namesExtractor.getU32 (&offset, &err);
2770
+ attr.attrSize = 4 ;
2771
+ ie->parentOffset = entriesBase + attr.attrValue ;
2772
+ } else if (a.Form != DW_FORM_flag_present) {
2773
+ errMsg = " : invalid form for DW_IDX_parent" ;
2774
+ }
2775
+ } else {
2776
+ switch (a.Form ) {
2777
+ case DW_FORM_data1:
2778
+ case DW_FORM_ref1: {
2779
+ attr.attrValue = namesExtractor.getU8 (&offset, &err);
2780
+ attr.attrSize = 1 ;
2781
+ break ;
2782
+ }
2783
+ case DW_FORM_data2:
2784
+ case DW_FORM_ref2: {
2785
+ attr.attrValue = namesExtractor.getU16 (&offset, &err);
2786
+ attr.attrSize = 2 ;
2787
+ break ;
2788
+ }
2789
+ case DW_FORM_data4:
2790
+ case DW_FORM_ref4: {
2791
+ attr.attrValue = namesExtractor.getU32 (&offset, &err);
2792
+ attr.attrSize = 4 ;
2793
+ break ;
2794
+ }
2795
+ default :
2796
+ errMsg = " : unrecognized form encoding " ;
2797
+ errMsg.append (std::to_string (a.Form ));
2798
+ errMsg.append (" in abbrev table" );
2799
+ return createStringError (inconvertibleErrorCode (), errMsg.c_str ());
2800
+ }
2801
+ }
2802
+ if (err) {
2803
+ errMsg = " : error while reading attributes: " ;
2804
+ errMsg.append (toString (std::move (err)));
2805
+ return createStringError (inconvertibleErrorCode (), errMsg.c_str ());
2806
+ }
2807
+ if (a.Index == DW_IDX_compile_unit)
2808
+ cuAttr = attr;
2809
+ else if (a.Form != DW_FORM_flag_present)
2810
+ ie->attrValues .push_back (attr);
2811
+ }
2812
+
2813
+ // Canonicalize abbrev by placing the CU/TU index at the end.
2814
+ ie->attrValues .push_back (cuAttr);
2815
+
2816
+ if (!errMsg.empty ())
2817
+ return createStringError (inconvertibleErrorCode (), errMsg.c_str ());
2818
+ else
2819
+ return ie;
2820
+ }
2821
+
2737
2822
void DebugNamesBaseSection::parseDebugNames (
2738
2823
InputChunk &inputChunk, OutputChunk &chunk,
2739
2824
DWARFDataExtractor &namesExtractor, DataExtractor &strExtractor,
2740
2825
function_ref<SmallVector<uint32_t , 0 >(
2741
2826
uint32_t numCus, const DWARFDebugNames::Header &,
2742
2827
const DWARFDebugNames::DWARFDebugNamesOffsets &)>
2743
2828
readOffsets) {
2744
- const LLDDWARFSection namesSec = inputChunk.section ;
2829
+ const LLDDWARFSection & namesSec = inputChunk.section ;
2745
2830
DenseMap<uint32_t , IndexEntry *> offsetMap;
2746
2831
// Number of CUs seen in previous NameIndex sections within current chunk.
2747
2832
uint32_t numCus = 0 ;
@@ -2758,8 +2843,7 @@ void DebugNamesBaseSection::parseDebugNames(
2758
2843
Twine (nd.hdr .Version ));
2759
2844
return ;
2760
2845
}
2761
- const uint32_t dwarfSize =
2762
- dwarf::getDwarfOffsetByteSize (DwarfFormat::DWARF32);
2846
+ uint32_t dwarfSize = dwarf::getDwarfOffsetByteSize (DwarfFormat::DWARF32);
2763
2847
DWARFDebugNames::DWARFDebugNamesOffsets locs = ni.getOffsets ();
2764
2848
if (locs.EntriesBase > namesExtractor.getData ().size ()) {
2765
2849
errorOrWarn (toString (namesSec.sec ) +
@@ -2782,83 +2866,23 @@ void DebugNamesBaseSection::parseDebugNames(
2782
2866
ne.hashValue = caseFoldingDjbHash (name);
2783
2867
2784
2868
// Read a series of index entries that end with abbreviation code 0.
2785
- const char * errMsg = nullptr ;
2869
+ std::string errMsg;
2786
2870
uint64_t offset = locs.EntriesBase + entryOffsets[i];
2787
2871
while (offset < namesSec.Data .size () && namesSec.Data [offset] != 0 ) {
2788
2872
// Read & store all entries (for the same string).
2789
- auto ie = makeThreadLocal<IndexEntry>();
2790
- ie->poolOffset = offset;
2791
- Error err = Error::success ();
2792
- ie->abbrevCode =
2793
- static_cast <uint32_t >(namesExtractor.getULEB128 (&offset, &err));
2794
- if (err) {
2795
- consumeError (std::move (err));
2796
- errMsg = " : invalid abbrev code in entry" ;
2797
- break ;
2873
+ Expected<IndexEntry *> ieOrErr =
2874
+ readEntry (offset, ni, locs.EntriesBase , namesExtractor, namesSec);
2875
+ if (!ieOrErr) {
2876
+ errorOrWarn (toString (namesSec.sec ) +
2877
+ Twine (toString (ieOrErr.takeError ())));
2878
+ return ;
2798
2879
}
2799
- auto it = ni.getAbbrevs ().find_as (ie->abbrevCode );
2800
- if (it == ni.getAbbrevs ().end ()) {
2801
- errMsg = " : invalid abbrev code in entry" ;
2802
- break ;
2803
- }
2804
-
2805
- AttrValue attr, cuAttr = {0 , 0 };
2806
- for (DWARFDebugNames::AttributeEncoding a : it->Attributes ) {
2807
- if (a.Index == dwarf::DW_IDX_parent) {
2808
- if (a.Form == dwarf::DW_FORM_ref4) {
2809
- attr.attrValue = namesExtractor.getU32 (&offset, &err);
2810
- attr.attrSize = 4 ;
2811
- ie->parentOffset = locs.EntriesBase + attr.attrValue ;
2812
- } else if (a.Form != DW_FORM_flag_present) {
2813
- errMsg = " : invalid form for DW_IDX_parent" ;
2814
- }
2815
- } else {
2816
- switch (a.Form ) {
2817
- case DW_FORM_data1:
2818
- case DW_FORM_ref1: {
2819
- attr.attrValue = namesExtractor.getU8 (&offset, &err);
2820
- attr.attrSize = 1 ;
2821
- break ;
2822
- }
2823
- case DW_FORM_data2:
2824
- case DW_FORM_ref2: {
2825
- attr.attrValue = namesExtractor.getU16 (&offset, &err);
2826
- attr.attrSize = 2 ;
2827
- break ;
2828
- }
2829
- case DW_FORM_data4:
2830
- case DW_FORM_ref4: {
2831
- attr.attrValue = namesExtractor.getU32 (&offset, &err);
2832
- attr.attrSize = 4 ;
2833
- break ;
2834
- }
2835
- default :
2836
- errorOrWarn (toString (namesSec.sec ) +
2837
- Twine (" : unrecognized form encoding " ) +
2838
- Twine (a.Form ) + Twine (" in abbrev table" ));
2839
- return ;
2840
- }
2841
- }
2842
- if (err) {
2843
- errorOrWarn (toString (namesSec.sec ) +
2844
- Twine (" : error while reading attributes: " ) +
2845
- toString (std::move (err)));
2846
- return ;
2847
- }
2848
- if (a.Index == DW_IDX_compile_unit)
2849
- cuAttr = attr;
2850
- else if (a.Form != DW_FORM_flag_present)
2851
- ie->attrValues .push_back (attr);
2852
- }
2853
-
2854
- // Canonicalize abbrev by placing the CU/TU index at the end.
2855
- ie->attrValues .push_back (cuAttr);
2856
- ne.indexEntries .push_back (std::move (ie));
2880
+ ne.indexEntries .push_back (std::move (*ieOrErr));
2857
2881
}
2858
2882
if (offset >= namesSec.Data .size ())
2859
2883
errMsg = " : index entry is out of bounds" ;
2860
- if (errMsg)
2861
- errorOrWarn (toString (namesSec.sec ) + Twine (errMsg));
2884
+ if (! errMsg. empty () )
2885
+ errorOrWarn (toString (namesSec.sec ) + Twine (errMsg. c_str () ));
2862
2886
2863
2887
for (IndexEntry &ie : ne.entries ())
2864
2888
offsetMap[ie.poolOffset ] = &ie;
@@ -2904,12 +2928,8 @@ void DebugNamesBaseSection::computeHdrAndAbbrevTable(
2904
2928
numCu += chunks[i].compUnits .size ();
2905
2929
for (const NameData &nd : inputChunk.nameData ) {
2906
2930
hdr.CompUnitCount += nd.hdr .CompUnitCount ;
2907
- // We are not actually handling or emitting type units yet, so
2908
- // so non-zero type unit counts will crash LLD.
2909
- // TODO: Uncomment the two lines below when we implement this for
2910
- // type units & remove the following check/warning.
2911
- // hdr.LocalTypeUnitCount += nd.hdr.LocalTypeUnitCount;
2912
- // hdr.ForeignTypeUnitCount += nd.hdr.ForeignTypeUnitCount;
2931
+ // TODO: We don't handle type units yet, so LocalTypeUnitCount &
2932
+ // ForeignTypeUnitCount are left as 0.
2913
2933
if (nd.hdr .LocalTypeUnitCount || nd. hdr.ForeignTypeUnitCount )
2914
2934
warn (toString (inputChunk.section .sec ) +
2915
2935
Twine (" : type units are not implemented" ));
@@ -2932,7 +2952,7 @@ void DebugNamesBaseSection::computeHdrAndAbbrevTable(
2932
2952
FoldingSet<Abbrev> abbrevSet;
2933
2953
// Determine the form for the DW_IDX_compile_unit attributes in the merged
2934
2954
// index. The input form may not be big enough for all CU indices.
2935
- const dwarf::Form cuAttrForm = getMergedCuCountForm (hdr.CompUnitCount ).second ;
2955
+ dwarf::Form cuAttrForm = getMergedCuCountForm (hdr.CompUnitCount ).second ;
2936
2956
for (InputChunk &inputChunk : inputChunks) {
2937
2957
for (auto [i, ni] : enumerate(*inputChunk.llvmDebugNames )) {
2938
2958
for (const DWARFDebugNames::Abbrev &oldAbbrev : ni.getAbbrevs ()) {
@@ -3003,10 +3023,10 @@ std::pair<uint32_t, uint32_t> DebugNamesBaseSection::computeEntryPool(
3003
3023
// Collect and de-duplicate all the names (preserving all the entries).
3004
3024
// Speed it up using multithreading, as the number of symbols can be in the
3005
3025
// order of millions.
3006
- const size_t concurrency =
3026
+ size_t concurrency =
3007
3027
bit_floor (std::min<size_t >(config->threadCount , numShards));
3008
- const size_t shift = 32 - countr_zero (numShards);
3009
- const uint8_t cuAttrSize = getMergedCuCountForm (hdr.CompUnitCount ).first ;
3028
+ size_t shift = 32 - countr_zero (numShards);
3029
+ uint8_t cuAttrSize = getMergedCuCountForm (hdr.CompUnitCount ).first ;
3010
3030
DenseMap<CachedHashStringRef, size_t > maps[numShards];
3011
3031
3012
3032
parallelFor (0 , concurrency, [&](size_t threadId) {
@@ -3237,8 +3257,8 @@ template <class ELFT> void DebugNamesSection<ELFT>::writeTo(uint8_t *buf) {
3237
3257
buf += hdr.AugmentationStringSize ;
3238
3258
3239
3259
// Write the CU list.
3240
- for (auto i : seq (numChunks ))
3241
- for (uint32_t cuOffset : chunks[i] .compUnits )
3260
+ for (auto &chunk : getChunks ( ))
3261
+ for (uint32_t cuOffset : chunk .compUnits )
3242
3262
endian::writeNext<uint32_t , ELFT::Endianness>(buf, cuOffset);
3243
3263
3244
3264
// TODO: Write the local TU list, then the foreign TU list..
0 commit comments