Skip to content

Commit

Permalink
Pointers in sections and directories classes should be under smar…
Browse files Browse the repository at this point in the history
…t pointers
  • Loading branch information
Unkorunk authored and serge1 committed Nov 16, 2024
1 parent 722c6ef commit bf7cfaf
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 118 deletions.
94 changes: 48 additions & 46 deletions coffi/coffi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ class coffi : public coffi_strings,
}
coff_header_->set_optional_header_size(narrow_cast<uint16_t>(
optional_header_->get_sizeof() + win_header_->get_sizeof() +
directories_.size() * sizeof(image_data_directory)));
directories_.get_count() * sizeof(image_data_directory)));
}

//---------------------------------------------------------------------
Expand Down Expand Up @@ -485,17 +485,18 @@ class coffi : public coffi_strings,
*/
section* add_section(const std::string& name)
{
section* sec;
std::unique_ptr<section> sec;
if (architecture_ == COFFI_ARCHITECTURE_TI) {
sec = new section_impl_ti{this, this, this};
sec = std::make_unique<section_impl_ti>(this, this, this);
}
else {
sec = new section_impl{this, this, this};
sec = std::make_unique<section_impl>(this, this, this);
}
sec->set_index(narrow_cast<uint32_t>(sections_.size()));
sec->set_index(narrow_cast<uint32_t>(sections_.get_count()));
sec->set_name(name);
sections_.push_back(sec);
return sec;
section* sec_ptr = sec.get();
sections_.append(std::move(sec));
return sec_ptr;
}

//---------------------------------------------------------------------
Expand All @@ -522,12 +523,13 @@ class coffi : public coffi_strings,
*/
directory* add_directory(const image_data_directory& rva_and_size)
{
directory* d =
new directory(narrow_cast<uint32_t>(directories_.size()));
std::unique_ptr<directory> d =
std::make_unique<directory>(narrow_cast<uint32_t>(directories_.get_count()));
d->set_virtual_address(rva_and_size.virtual_address);
d->set_size(rva_and_size.size);
directories_.push_back(d);
return d;
directory* d_ptr = d.get();
directories_.append(std::move(d));
return d_ptr;
}

//---------------------------------------------------------------------
Expand Down Expand Up @@ -658,7 +660,7 @@ class coffi : public coffi_strings,
return false;
}
sec->set_index(i);
sections_.push_back(sec.release());
sections_.append(std::move(sec));
}

return true;
Expand All @@ -676,7 +678,7 @@ class coffi : public coffi_strings,

// Compute the header fields
coff_header_->set_sections_count(
narrow_cast<uint16_t>(sections_.size()));
narrow_cast<uint16_t>(sections_.get_count()));
if (symbols_.size() > 0) {
coff_header_->set_symbols_count(
symbols_.back().get_index() +
Expand All @@ -695,8 +697,8 @@ class coffi : public coffi_strings,
uint32_t size_of_initialized_data = 0;
uint32_t size_of_uninitialized_data = 0;
for (const auto &section : sections_) {
const uint32_t flags = section->get_flags();
const uint32_t data_size = section->get_data_size();
const uint32_t flags = section.get_flags();
const uint32_t data_size = section.get_data_size();
if (flags & IMAGE_SCN_CNT_CODE) {
size_of_code += data_size;
}
Expand All @@ -714,7 +716,7 @@ class coffi : public coffi_strings,
}
if (win_header_) {
win_header_->set_number_of_rva_and_sizes(
narrow_cast<uint32_t>(directories_.size()));
narrow_cast<uint32_t>(directories_.get_count()));
coff_header_->set_optional_header_size(narrow_cast<uint16_t>(
coff_header_->get_optional_header_size() +
win_header_->get_sizeof() + directories_.get_sizeof()));
Expand All @@ -727,13 +729,13 @@ class coffi : public coffi_strings,
coff_header_->get_sizeof() +
coff_header_->get_optional_header_size();
for (const auto& section : sections_) {
size_of_headers += section->get_sizeof();
size_of_headers += section.get_sizeof();
}
size_of_headers = alignTo(size_of_headers, file_alignment);

uint32_t size_of_image = alignTo(size_of_headers, section_alignment);
for (const auto &section : sections_) {
const uint32_t virtual_size = section->get_virtual_size();
const uint32_t virtual_size = section.get_virtual_size();
if (virtual_size) {
size_of_image += alignTo(virtual_size, section_alignment);
}
Expand Down Expand Up @@ -761,8 +763,8 @@ class coffi : public coffi_strings,
}
}

for (auto sec : sections_) {
sec->save_header(stream);
for (auto &sec : sections_) {
sec.save_header(stream);
}

for (auto dp : data_pages_) {
Expand All @@ -784,7 +786,7 @@ class coffi : public coffi_strings,
directories_[dp.index]->save_data(stream);
break;
case DATA_PAGE_UNUSED:
stream.write(unused_spaces_[dp.index].data,
stream.write(unused_spaces_[dp.index].data.get(),
unused_spaces_[dp.index].size);
break;
}
Expand Down Expand Up @@ -843,29 +845,29 @@ class coffi : public coffi_strings,
void populate_data_pages()
{
data_pages_.clear();
for (auto sec : sections_) {
if (sec->get_data_offset() > 0 || sec->get_data()) {
for (auto &sec : sections_) {
if (sec.get_data_offset() > 0 || sec.get_data()) {
data_pages_.push_back(
data_page{DATA_PAGE_RAW, sec->get_data_offset(),
sec->get_data_size(), sec->get_index()});
data_page{DATA_PAGE_RAW, sec.get_data_offset(),
sec.get_data_size(), sec.get_index()});
}
if (sec->get_reloc_offset() > 0 || sec->get_reloc_count() > 0) {
if (sec.get_reloc_offset() > 0 || sec.get_reloc_count() > 0) {
data_pages_.push_back(data_page{
DATA_PAGE_RELOCATIONS, sec->get_reloc_offset(),
sec->get_relocations_filesize(), sec->get_index()});
DATA_PAGE_RELOCATIONS, sec.get_reloc_offset(),
sec.get_relocations_filesize(), sec.get_index()});
}
if ((architecture_ != COFFI_ARCHITECTURE_TI) &&
(sec->get_line_num_count() > 0)) {
(sec.get_line_num_count() > 0)) {
data_pages_.push_back(data_page{
DATA_PAGE_LINE_NUMBERS, sec->get_line_num_offset(),
sec->get_line_numbers_filesize(), sec->get_index()});
DATA_PAGE_LINE_NUMBERS, sec.get_line_num_offset(),
sec.get_line_numbers_filesize(), sec.get_index()});
}
}
for (auto d : directories_) {
if (d->get_data_filesize() > 0) {
for (auto& d : directories_) {
if (d.get_data_filesize() > 0) {
data_pages_.push_back(data_page{DATA_PAGE_DIRECTORY,
d->get_virtual_address(),
d->get_size(), d->get_index()});
d.get_virtual_address(),
d.get_size(), d.get_index()});
}
}

Expand Down Expand Up @@ -905,8 +907,8 @@ class coffi : public coffi_strings,
offset += narrow_cast<uint32_t>(win_header_->get_sizeof());
}
offset += directories_.get_sizeof();
for (auto sec : sections_) {
offset += narrow_cast<uint32_t>(sec->get_sizeof());
for (auto &sec : sections_) {
offset += narrow_cast<uint32_t>(sec.get_sizeof());
}
return offset;
}
Expand Down Expand Up @@ -982,10 +984,10 @@ class coffi : public coffi_strings,
file_alignment - (previous_size % file_alignment);
if (previous_dp && previous_dp->type == DATA_PAGE_RAW) {
// Extend the previous section data
char* padding = new char[size];
std::unique_ptr<char[]> padding = std::make_unique<char[]>(size);
if (padding) {
std::memset(padding, 0, size);
sections_[previous_dp->index]->append_data(padding,
std::memset(padding.get(), 0, size);
sections_[previous_dp->index]->append_data(padding.get(),
size);
}
}
Expand All @@ -1005,8 +1007,8 @@ class coffi : public coffi_strings,
//---------------------------------------------------------------------
void clean_unused_spaces()
{
for (auto us : unused_spaces_) {
delete[] us.data;
for (auto& us : unused_spaces_) {
us.data.reset();
}
unused_spaces_.clear();
}
Expand All @@ -1016,12 +1018,12 @@ class coffi : public coffi_strings,
add_unused_space(uint32_t offset, uint32_t size, uint8_t padding_byte = 0)
{
unused_space us;
us.data = new char[size];
us.data = std::make_unique<char[]>(size);
if (us.data) {
std::memset(us.data, padding_byte, size);
std::memset(us.data.get(), padding_byte, size);
us.size = size;
us.offset = offset;
unused_spaces_.push_back(us);
unused_spaces_.emplace_back(std::move(us));
}
}

Expand Down Expand Up @@ -1050,7 +1052,7 @@ class coffi : public coffi_strings,
{
uint32_t offset;
uint32_t size;
char* data;
std::unique_ptr<char[]> data;
};

//---------------------------------------------------------------------
Expand Down
42 changes: 13 additions & 29 deletions coffi/coffi_directory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,29 +169,17 @@ class directory
};

/*! @brief List of image data directories
*
* It is implemented as a vector of @ref directory pointers.
*/
class directories : public std::vector<directory*>
*
* It is implemented as a vector of @ref directory pointers.
*/
class directories : public unique_ptr_collection<directory>
{
public:
//------------------------------------------------------------------------------
directories(sections_provider* scn) : scn_{scn} {}
explicit directories(sections_provider* scn) : scn_{scn} {}

//! Discards the copy constructor
directories(const directories&) = delete;

virtual ~directories() { clean(); }

void clean()
{
for (auto d : *this) {
delete d;
}
clear();
}

//------------------------------------------------------------------------------
bool load(std::istream& stream)
{
for (uint32_t i = 0;
Expand All @@ -200,42 +188,38 @@ class directories : public std::vector<directory*>
if (!d->load(stream)) {
return false;
}
push_back(d.release());
append(std::move(d));
}
return true;
}

//------------------------------------------------------------------------------
bool load_data(std::istream& stream)
{
for (auto d : *this) {
if (!d->load_data(stream)) {
for (auto& d : *this) {
if (!d.load_data(stream)) {
return false;
}
}
return true;
}

//---------------------------------------------------------------------
void save(std::ostream& stream) const
{
for (auto d : *this) {
d->save(stream);
for (const auto& d : *this) {
d.save(stream);
}
}

//------------------------------------------------------------------------------
uint32_t get_sizeof() const
{
if (size() > 0) {
return narrow_cast<uint32_t>(size() * (*begin())->get_sizeof());
if (get_count() > 0) {
return narrow_cast<uint32_t>(get_count() * (*begin()).get_sizeof());
}

return 0;
}

//------------------------------------------------------------------------------
protected:
//------------------------------------------------------------------------------
sections_provider* scn_;
};

Expand Down
Loading

0 comments on commit bf7cfaf

Please sign in to comment.