Skip to content

Commit

Permalink
Clean and simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
romainthomas committed Jul 25, 2023
1 parent 3dfc393 commit 97bf8b6
Show file tree
Hide file tree
Showing 45 changed files with 159 additions and 193 deletions.
4 changes: 2 additions & 2 deletions api/python/src/PE/objects/debug/pyCodeViewPDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ void create<CodeViewPDB>(nb::module_& m) {

.def_prop_rw("signature",
nb::overload_cast<>(&CodeViewPDB::signature, nb::const_),
nb::overload_cast<CodeViewPDB::signature_t>(&CodeViewPDB::signature))
nb::overload_cast<const CodeViewPDB::signature_t&>(&CodeViewPDB::signature))

.def_prop_rw("age",
nb::overload_cast<>(&CodeViewPDB::age, nb::const_),
nb::overload_cast<uint32_t>(&CodeViewPDB::age))

.def_prop_rw("filename",
nb::overload_cast<>(&CodeViewPDB::filename, nb::const_),
nb::overload_cast<const std::string&>(&CodeViewPDB::filename),
nb::overload_cast<std::string>(&CodeViewPDB::filename),
"Original pdb path")

LIEF_DEFAULT_STR(CodeViewPDB);
Expand Down
8 changes: 8 additions & 0 deletions include/LIEF/BinaryStream/BinaryStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,14 @@ class ScopedStream {
return &stream_;
}

BinaryStream& operator*() {
return stream_;
}

const BinaryStream& operator*() const {
return stream_;
}

private:
uint64_t pos_ = 0;
BinaryStream& stream_;
Expand Down
5 changes: 3 additions & 2 deletions include/LIEF/PE/Import.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ class LIEF_API Import : public Object {
~Import() override;

Import(const Import& other);
Import& operator=(Import other);
void swap(Import& other);
Import(Import&& other);
Import& operator=(Import&& other);
Import& operator=(const Import& other);

//! The index of the first forwarder reference
uint32_t forwarder_chain() const;
Expand Down
1 change: 0 additions & 1 deletion include/LIEF/PE/RichEntry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#define LIEF_PE_RICH_ENTRY_H
#include <cstdint>
#include <ostream>
#include <cstdint>

#include "LIEF/Object.hpp"
#include "LIEF/visibility.h"
Expand Down
6 changes: 3 additions & 3 deletions include/LIEF/PE/debug/CodeViewPDB.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ class LIEF_API CodeViewPDB : public CodeView {
age_ = age;
}

void signature(signature_t sig) {
signature_ = std::move(sig);
void signature(const signature_t& sig) {
signature_ = sig;
}

void filename(const std::string& filename) {
void filename(std::string filename) {
filename_ = std::move(filename);
}

Expand Down
2 changes: 2 additions & 0 deletions include/LIEF/PE/debug/PogoEntry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class LIEF_API PogoEntry : public Object {

public:
PogoEntry();
PogoEntry(PogoEntry&& other);
PogoEntry& operator=(PogoEntry&& other);
PogoEntry(const PogoEntry&);
PogoEntry(uint32_t start_rva, uint32_t size, std::string name) :
start_rva_{start_rva},
Expand Down
22 changes: 7 additions & 15 deletions include/LIEF/iterators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ class ref_iterator {
using pointer_t = typename ref_iterator::pointer;

ref_iterator(T container) :
container_{std::forward<T>(container)},
distance_{0}
container_{std::forward<T>(container)}
{
it_ = std::begin(container_);
}
Expand Down Expand Up @@ -280,10 +279,8 @@ class filter_iterator {
using filter_t = std::function<bool (const typename DT::value_type&)>;

filter_iterator(T container, filter_t filter) :
size_c_{0},
container_{std::forward<T>(container)},
filters_{},
distance_{0}
filters_{}
{

it_ = std::begin(container_);
Expand All @@ -299,10 +296,8 @@ class filter_iterator {
}

filter_iterator(T container, const std::vector<filter_t>& filters) :
size_c_{0},
container_{std::forward<T>(container)},
filters_{filters},
distance_{0}
filters_{filters}
{

it_ = std::begin(container_);
Expand All @@ -315,16 +310,13 @@ class filter_iterator {
}

filter_iterator(T container) :
size_c_{0},
container_{std::forward<T>(container)},
filters_{},
distance_{0}
filters_{}
{
it_ = std::begin(container_);
}

filter_iterator(const filter_iterator& copy) :
size_c_{0},
container_{copy.container_},
it_{std::begin(container_)},
filters_{copy.filters_},
Expand Down Expand Up @@ -429,7 +421,7 @@ class filter_iterator {
}

size_t size() const {
if (filters_.size() == 0) {
if (filters_.empty()) {
return container_.size();
}

Expand Down Expand Up @@ -476,11 +468,11 @@ class filter_iterator {
}


mutable size_t size_c_;
mutable size_t size_c_ = 0;
T container_;
ITERATOR_T it_;
std::vector<filter_t> filters_;
typename filter_iterator::difference_type distance_;
typename filter_iterator::difference_type distance_ = 0;
};

//! Iterator which return a const ref on container's values given predicates
Expand Down
4 changes: 2 additions & 2 deletions src/Abstract/Section.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ size_t Section::search(uint64_t integer, size_t pos, size_t size) const {
size_t Section::search(const std::vector<uint8_t>& pattern, size_t pos) const {
span<const uint8_t> content = this->content();

const auto it_found = std::search(
const auto* it_found = std::search(
std::begin(content) + pos, std::end(content),
std::begin(pattern), std::end(pattern));

Expand Down Expand Up @@ -179,7 +179,7 @@ std::vector<size_t> Section::search_all(const std::string& v) const {
double Section::entropy() const {
std::array<uint64_t, 256> frequencies = { {0} };
span<const uint8_t> content = this->content();
if (content.size() == 0) {
if (content.empty()) {
return 0.;
}
for (uint8_t x : content) {
Expand Down
19 changes: 7 additions & 12 deletions src/ELF/Binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1144,21 +1144,16 @@ bool Binary::is_pie() const {


bool Binary::has_nx() const {
const auto it_stack = std::find_if(std::begin(segments_), std::end(segments_),
[] (const std::unique_ptr<Segment>& segment) {
return segment->type() == SEGMENT_TYPES::PT_GNU_STACK;
});
if (it_stack == std::end(segments_)) {
if (header().machine_type() == ARCH::EM_PPC64) {
// The PPC64 ELF ABI has a non-executable stack by default.
return true;
} else {
return false;
}
if (const Segment* gnu_stack = get(SEGMENT_TYPES::PT_GNU_STACK)) {
return gnu_stack->has(ELF_SEGMENT_FLAGS::PF_X);
}

return !(*it_stack)->has(ELF_SEGMENT_FLAGS::PF_X);
if (header().machine_type() == ARCH::EM_PPC64) {
// The PPC64 ELF ABI has a non-executable stack by default.
return true;
}

return false;
}

Segment* Binary::add(const Segment& segment, uint64_t base) {
Expand Down
30 changes: 15 additions & 15 deletions src/ELF/ExeLayout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LIEF_ELF_EXE_LAYOUT_H_
#define LIEF_ELF_EXE_LAYOUT_H_
#ifndef LIEF_ELF_EXE_LAYOUT_H
#define LIEF_ELF_EXE_LAYOUT_H
#include <LIEF/types.hpp>
#include <LIEF/visibility.h>
#include <LIEF/ELF/Binary.hpp>
Expand Down Expand Up @@ -194,12 +194,12 @@ class LIEF_LOCAL ExeLayout : public Layout {

template<class ELF_T>
size_t dynamic_arraysize(DYNAMIC_TAGS tag) {
using uint__ = typename ELF_T::uint;
using uint = typename ELF_T::uint;
DynamicEntry* entry = binary_->get(tag);
if (entry == nullptr || !DynamicEntryArray::classof(entry)) {
return 0;
}
return entry->as<const DynamicEntryArray&>()->size() * sizeof(uint__);
return entry->as<const DynamicEntryArray&>()->size() * sizeof(uint);
}

template<class ELF_T>
Expand Down Expand Up @@ -280,7 +280,7 @@ class LIEF_LOCAL ExeLayout : public Layout {
//
// See also:
// * p.9, https://www.akkadia.org/drepper/dsohowto.pdf
using uint__ = typename ELF_T::uint;
using uint = typename ELF_T::uint;
if (!raw_gnu_hash_.empty()) {
return raw_gnu_hash_.size();
}
Expand Down Expand Up @@ -327,7 +327,7 @@ class LIEF_LOCAL ExeLayout : public Layout {
vector_iostream raw_gnuhash;
raw_gnuhash.reserve(
4 * sizeof(uint32_t) + // header
maskwords * sizeof(uint__) + // bloom filters
maskwords * sizeof(uint) + // bloom filters
nb_buckets * sizeof(uint32_t) + // buckets
(dynamic_symbols.size() - symndx) * sizeof(uint32_t)); // hash values

Expand All @@ -341,14 +341,14 @@ class LIEF_LOCAL ExeLayout : public Layout {

// Compute Bloom filters
// =================================
std::vector<uint__> bloom_filters(maskwords, 0);
size_t C = sizeof(uint__) * 8; // 32 for ELF, 64 for ELF64
std::vector<uint> bloom_filters(maskwords, 0);
size_t C = sizeof(uint) * 8; // 32 for ELF, 64 for ELF64

for (size_t i = symndx; i < dynamic_symbols.size(); ++i) {
const uint32_t hash = dl_new_hash(dynamic_symbols[i].name().c_str());
const size_t pos = (hash / C) & (gnu_hash->maskwords() - 1);
uint__ V = (static_cast<uint__>(1) << (hash % C)) |
(static_cast<uint__>(1) << ((hash >> gnu_hash->shift2()) % C));
uint V = (static_cast<uint>(1) << (hash % C)) |
(static_cast<uint>(1) << ((hash >> gnu_hash->shift2()) % C));
bloom_filters[pos] |= V;
}
for (size_t idx = 0; idx < bloom_filters.size(); ++idx) {
Expand Down Expand Up @@ -559,7 +559,7 @@ class LIEF_LOCAL ExeLayout : public Layout {
interp_segment.type(SEGMENT_TYPES::PT_INTERP);
interp_segment.add(ELF_SEGMENT_FLAGS::PF_R);
interp_segment.content(std::vector<uint8_t>(interp_size_));
if (auto interp = binary_->add(interp_segment)) {
if (Segment* interp = binary_->add(interp_segment)) {
LIEF_DEBUG("Interp Segment: 0x{:x}:0x{:x}", interp->virtual_address(), interp->virtual_size());
} else {
LIEF_ERR("Can't add a new PT_INTERP");
Expand Down Expand Up @@ -685,7 +685,7 @@ class LIEF_LOCAL ExeLayout : public Layout {

Section* section = nullptr;
Segment::it_sections sections = pt_interp->sections();
if (sections.size() > 0) {
if (!sections.empty()) {
section = &sections[0];
}
pt_interp->virtual_address(va_r_base);
Expand Down Expand Up @@ -1042,7 +1042,7 @@ class LIEF_LOCAL ExeLayout : public Layout {
LIEF_ERR("DT_INIT_ARRAY not found");
return make_error_code(lief_errors::file_format_error);
}
DynamicEntryArray* dt_init_array = raw_dt_init->as<DynamicEntryArray>();
auto* dt_init_array = raw_dt_init->as<DynamicEntryArray>();
DynamicEntry* dt_init_arraysz = binary_->get(DYNAMIC_TAGS::DT_INIT_ARRAYSZ);

if (dt_init_arraysz == nullptr) {
Expand Down Expand Up @@ -1098,7 +1098,7 @@ class LIEF_LOCAL ExeLayout : public Layout {
LIEF_ERR("DT_PREINIT_ARRAY not found");
return make_error_code(lief_errors::file_format_error);
}
DynamicEntryArray* dt_preinit_array = raw_dt_preinit->as<DynamicEntryArray>();
auto* dt_preinit_array = raw_dt_preinit->as<DynamicEntryArray>();
DynamicEntry* dt_preinit_arraysz = binary_->get(DYNAMIC_TAGS::DT_PREINIT_ARRAYSZ);

if (dt_preinit_array == nullptr) {
Expand Down Expand Up @@ -1151,7 +1151,7 @@ class LIEF_LOCAL ExeLayout : public Layout {
LIEF_ERR("DT_FINI_ARRAY not found");
return make_error_code(lief_errors::file_format_error);
}
DynamicEntryArray* dt_fini_array = raw_dt_fini->as<DynamicEntryArray>();
auto* dt_fini_array = raw_dt_fini->as<DynamicEntryArray>();
DynamicEntry* dt_fini_arraysz = binary_->get(DYNAMIC_TAGS::DT_FINI_ARRAYSZ);

if (dt_fini_arraysz == nullptr) {
Expand Down
4 changes: 2 additions & 2 deletions src/ELF/Layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ size_t Layout::section_shstr_size() {

if (!binary_->static_symbols_.empty()) {
if (binary_->get(ELF_SECTION_TYPES::SHT_SYMTAB) == nullptr) {
sec_names.push_back(".symtab");
sec_names.emplace_back(".symtab");
}
if (binary_->get(ELF_SECTION_TYPES::SHT_SYMTAB) == nullptr) {
sec_names.push_back(".strtab");
sec_names.emplace_back(".strtab");
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/ELF/Layout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LIEF_ELF_LAYOUT_H_
#define LIEF_ELF_LAYOUT_H_
#ifndef LIEF_ELF_LAYOUT_H
#define LIEF_ELF_LAYOUT_H

#include <cstdint>
#include <unordered_map>
#include <string>
#include <vector>

namespace LIEF {
namespace ELF {
class Section;
Expand Down
5 changes: 3 additions & 2 deletions src/ELF/ObjectFileLayout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LIEF_ELF_OBJECT_FILE_LAYOUT_H_
#define LIEF_ELF_OBJECT_FILE_LAYOUT_H_
#ifndef LIEF_ELF_OBJECT_FILE_LAYOUT_H
#define LIEF_ELF_OBJECT_FILE_LAYOUT_H

#include <LIEF/types.hpp>
#include <LIEF/visibility.h>
#include <LIEF/ELF/Binary.hpp>
Expand Down
16 changes: 0 additions & 16 deletions src/ELF/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include "logging.hpp"


#include "LIEF/BinaryStream/VectorStream.hpp"

#include "LIEF/ELF/utils.hpp"
Expand All @@ -40,21 +39,6 @@
namespace LIEF {
namespace ELF {

constexpr uint32_t Parser::DELTA_NB_SYMBOLS;
constexpr uint32_t Parser::MAX_NOTE_DESCRIPTION;
constexpr uint32_t Parser::NB_MAX_BUCKETS;
constexpr uint32_t Parser::NB_MAX_CHAINS;
constexpr uint32_t Parser::NB_MAX_DYNAMIC_ENTRIES;
constexpr uint32_t Parser::NB_MAX_MASKWORD;
constexpr uint32_t Parser::NB_MAX_RELOCATIONS;
constexpr uint32_t Parser::NB_MAX_SEGMENTS;
constexpr uint32_t Parser::NB_MAX_SYMBOLS;
constexpr uint32_t Parser::MAX_SEGMENT_SIZE;
constexpr uint32_t Parser::MAX_SECTION_SIZE;

constexpr const char AndroidNote::NAME[];


Parser::~Parser() = default;
Parser::Parser() = default;

Expand Down
4 changes: 2 additions & 2 deletions src/ELF/Segment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,10 +477,10 @@ std::ostream& operator<<(std::ostream& os, const ELF::Segment& segment) {
<< std::setw(10) << segment.alignment()
<< std::endl;

if (segment.sections().size() > 0) {
if (!segment.sections().empty()) {
os << "Sections in this segment :" << std::endl;
for (const Section& section : segment.sections()) {
os << "\t" << section.name() << std::endl;
os << " " << section.name() << std::endl;
}
}
return os;
Expand Down
Loading

0 comments on commit 97bf8b6

Please sign in to comment.