Make StringTable move only
Also add c-tor for vector<char> which assumes
the ownership of a buffer containing strtab.
Bug: http://b/321641092
Test: builds
Change-Id: I7394c58e6221b101b4b5cf5813a73fb7e6a428de
diff --git a/tools/nogrod/dwarf_context.h b/tools/nogrod/dwarf_context.h
index aa027fb..1023f00 100644
--- a/tools/nogrod/dwarf_context.h
+++ b/tools/nogrod/dwarf_context.h
@@ -18,6 +18,7 @@
#define NOGROD_DWARF_CONTEXT_
#include <cstdint>
+#include <optional>
#include <string>
#include <berberis/base/macros.h>
@@ -31,7 +32,7 @@
class DwarfContext {
public:
DwarfContext(ByteInputStream* dwarf_info_stream,
- StringTable* debug_str_table,
+ const StringTable* debug_str_table,
std::optional<StringOffsetTable> string_offset_table)
: dwarf_info_stream_{dwarf_info_stream},
debug_str_table_{debug_str_table},
@@ -58,7 +59,7 @@
private:
ByteInputStream* dwarf_info_stream_;
- StringTable* debug_str_table_;
+ const StringTable* debug_str_table_;
std::optional<StringOffsetTable> string_offset_table_;
std::optional<uint64_t> str_offsets_base_{};
};
diff --git a/tools/nogrod/dwarf_info.cc b/tools/nogrod/dwarf_info.cc
index 2e7c045..0e2a4f3 100644
--- a/tools/nogrod/dwarf_info.cc
+++ b/tools/nogrod/dwarf_info.cc
@@ -35,7 +35,7 @@
size_t abbrev_size,
const uint8_t* info,
size_t info_size,
- StringTable debug_str_table,
+ const StringTable* debug_str_table,
std::optional<StringOffsetTable> string_offset_table)
: abbrev_{abbrev},
abbrev_size_{abbrev_size},
@@ -49,7 +49,7 @@
std::unordered_map<uint64_t, std::unique_ptr<DwarfDie>>* die_map,
std::string* error_msg) {
ByteInputStream bs(info_, info_size_);
- DwarfContext context(&bs, &debug_str_table_, string_offset_table_);
+ DwarfContext context(&bs, debug_str_table_, string_offset_table_);
while (bs.available()) {
std::unique_ptr<DwarfCompilationUnit> cu = ReadCompilationUnit(&context, die_map, error_msg);
@@ -309,7 +309,7 @@
uint64_t abbrev_size_;
const uint8_t* info_;
uint64_t info_size_;
- StringTable debug_str_table_;
+ const StringTable* debug_str_table_;
std::optional<StringOffsetTable> string_offset_table_;
std::unordered_map<uint64_t, std::unordered_map<uint64_t, DwarfAbbrev>> abbrevs_;
@@ -340,11 +340,12 @@
abbrev_size_{abbrev_size},
info_{info},
info_size_{info_size},
- string_table_{string_table},
+ string_table_{std::move(string_table)},
string_offset_table_{std::move(string_offset_table)} {}
bool DwarfInfo::Parse(std::string* error_msg) {
- DwarfParser parser(abbrev_, abbrev_size_, info_, info_size_, string_table_, string_offset_table_);
+ DwarfParser parser(
+ abbrev_, abbrev_size_, info_, info_size_, &string_table_, string_offset_table_);
if (!parser.ReadDwarfInfo(&compilation_units_, &die_offset_map_, error_msg)) {
return false;
}
diff --git a/tools/nogrod/elf_reader.cc b/tools/nogrod/elf_reader.cc
index 4f8a951..b096849 100644
--- a/tools/nogrod/elf_reader.cc
+++ b/tools/nogrod/elf_reader.cc
@@ -379,7 +379,7 @@
dwarf_abbrev_shdr->sh_size,
ShdrOffsetToAddr<const uint8_t>(dwarf_info_shdr),
dwarf_info_shdr->sh_size,
- string_table,
+ std::move(string_table),
string_offsets_table));
if (!dwarf_info->Parse(error_msg)) {
diff --git a/tools/nogrod/string_table.h b/tools/nogrod/string_table.h
index 65cab88..b84cc3a 100644
--- a/tools/nogrod/string_table.h
+++ b/tools/nogrod/string_table.h
@@ -19,6 +19,7 @@
#include <cstdint>
#include <string>
+#include <vector>
#include <berberis/base/checks.h>
#include <berberis/base/macros.h>
@@ -34,8 +35,15 @@
CHECK(strtab_[strtab_size_ - 1] == 0);
}
- StringTable(const StringTable&) = default;
- StringTable& operator=(const StringTable&) = default;
+ explicit StringTable(std::vector<char> buffer) : buffer_{std::move(buffer)} {
+ strtab_ = buffer_.data();
+ strtab_size_ = buffer_.size();
+ // string table should be \0 terminated.
+ CHECK(strtab_[strtab_size_ - 1] == 0);
+ }
+
+ StringTable(const StringTable&) = delete;
+ StringTable& operator=(const StringTable&) = delete;
StringTable(StringTable&&) = default;
StringTable& operator=(StringTable&&) = default;
@@ -45,6 +53,7 @@
}
private:
+ std::vector<char> buffer_;
const char* strtab_;
size_t strtab_size_;
};