Skip to content

Commit 3461c6b

Browse files
author
Michal Bandzi
committed
Merge branch 'elf-core-support'
2 parents 681372d + 2574bd1 commit 3461c6b

31 files changed

+2367
-10
lines changed

include/retdec/fileformat/fftypes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "retdec/fileformat/types/dynamic_table/dynamic_table.h"
1515
#include "retdec/fileformat/types/export_table/export_table.h"
1616
#include "retdec/fileformat/types/import_table/import_table.h"
17+
#include "retdec/fileformat/types/note_section/elf_notes.h"
18+
#include "retdec/fileformat/types/note_section/elf_core.h"
1719
#include "retdec/fileformat/types/pdb_info/pdb_info.h"
1820
#include "retdec/fileformat/types/relocation_table/relocation_table.h"
1921
#include "retdec/fileformat/types/resource_table/resource_table.h"

include/retdec/fileformat/file_format/elf/elf_format.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <elfio/elfio.hpp>
1313

1414
#include "retdec/fileformat/file_format/file_format.h"
15+
#include "retdec/fileformat/types/note_section/elf_notes.h"
1516

1617
namespace retdec {
1718
namespace fileformat {
@@ -65,6 +66,13 @@ class ElfFormat : public FileFormat
6566
void loadSegments();
6667
void loadInfoFromDynamicTables(std::size_t noOfTables);
6768
void loadInfoFromDynamicSegment();
69+
void loadNoteSecSeg(ElfNoteSecSeg &noteSecSegs) const;
70+
void loadNotes();
71+
void loadCoreFileMap(std::size_t offset, std::size_t size);
72+
void loadCorePrStat(std::size_t offset, std::size_t size);
73+
void loadCorePrPsInfo(std::size_t offset, std::size_t size);
74+
void loadCoreAuxvInfo(std::size_t offset, std::size_t size);
75+
void loadCoreInfo();
6876
/// @}
6977
protected:
7078
int elfClass; ///< class of input ELF file

include/retdec/fileformat/file_format/file_format.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class FileFormat : public retdec::utils::ByteValueStorage, private retdec::utils
5858
std::vector<DynamicTable*> dynamicTables; ///< tables with dynamic records
5959
std::vector<unsigned char> bytes; ///< content of file as bytes
6060
std::vector<String> strings; ///< detected strings
61+
std::vector<ElfNoteSecSeg> noteSecSegs; ///< note sections or segemnts found in ELF file
6162
std::set<std::uint64_t> unknownRelocs; ///< unknown relocations
6263
ImportTable *importTable; ///< table of imports
6364
ExportTable *exportTable; ///< table of exports
@@ -66,6 +67,7 @@ class FileFormat : public retdec::utils::ByteValueStorage, private retdec::utils
6667
RichHeader *richHeader; ///< rich header
6768
PdbInfo *pdbInfo; ///< information about related PDB debug file
6869
CertificateTable *certificateTable; ///< table of certificates
70+
ElfCoreInfo *elfCoreInfo; ///< information about core file structures
6971
Format fileFormat; ///< format of input file
7072
bool stateIsValid; ///< internal state of instance
7173
std::vector<std::pair<std::size_t, std::size_t>> secHashInfo; ///< information for calculation of section table hash
@@ -202,6 +204,7 @@ class FileFormat : public retdec::utils::ByteValueStorage, private retdec::utils
202204
const RichHeader* getRichHeader() const;
203205
const PdbInfo* getPdbInfo() const;
204206
const CertificateTable* getCertificateTable() const;
207+
const ElfCoreInfo* getElfCoreInfo() const;
205208
const Symbol* getSymbol(const std::string &name) const;
206209
const Symbol* getSymbol(unsigned long long address) const;
207210
const Relocation* getRelocation(const std::string &name) const;
@@ -231,6 +234,7 @@ class FileFormat : public retdec::utils::ByteValueStorage, private retdec::utils
231234
const unsigned char* getBytesData() const;
232235
const unsigned char* getLoadedBytesData() const;
233236
const std::vector<String>& getStrings() const;
237+
const std::vector<ElfNoteSecSeg>& getElfNoteSecSegs() const;
234238
const std::set<std::uint64_t>& getUnknownRelocations() const;
235239
/// @}
236240

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* @file include/retdec/fileformat/types/note_section/elf_core.cpp
3+
* @brief Class for ELF core data.
4+
* @copyright (c) 2017 Avast Software, licensed under the MIT license
5+
*/
6+
7+
#ifndef RETDEC_FILEFORMAT_TYPES_NOTE_SECTION_ELF_CORE_H
8+
#define RETDEC_FILEFORMAT_TYPES_NOTE_SECTION_ELF_CORE_H
9+
10+
#include <string>
11+
#include <vector>
12+
#include <map>
13+
14+
#include "retdec/utils/address.h"
15+
16+
namespace retdec {
17+
namespace fileformat {
18+
19+
using RegisterMap = std::map<std::string, std::uint64_t>;
20+
using AuxVectorEntry = std::pair<std::uint64_t, std::uint64_t>;
21+
22+
/**
23+
* Entry for one mapped file in NT_FILE note
24+
*/
25+
class FileMapEntry
26+
{
27+
public:
28+
std::uint64_t startAddr; ///< start address of mapped segment
29+
std::uint64_t endAddr; ///< end address of mapped segment
30+
std::uint64_t pageOffset; ///< page offset
31+
std::string filePath; ///< full path to mapped file
32+
};
33+
34+
/**
35+
* Class representing one NT_PRSTATUS note
36+
*
37+
* @note This structure is far from complete but we will add support only for
38+
* things we can use somehow for decompilation purposes.
39+
*/
40+
class PrStatusInfo
41+
{
42+
public:
43+
std::uint64_t pid; ///< process ID
44+
std::uint64_t ppid; ///< parent process ID
45+
RegisterMap registers; ///< registers state
46+
};
47+
48+
/**
49+
* Class for representing information from core files
50+
*/
51+
class ElfCoreInfo
52+
{
53+
private:
54+
// NT_FILE
55+
std::uint64_t pageSize; ///< used page size
56+
std::vector<FileMapEntry> fileMap; ///< parsed file map
57+
58+
// NT_PRSTATUS
59+
std::vector<PrStatusInfo> prstatusInfos; ///< prstatus structures
60+
61+
// NT_PRPSINFO
62+
std::string appName; ///< original application name
63+
std::string cmdLine; ///< command line
64+
65+
// NT_AUXV
66+
std::vector<AuxVectorEntry> auxVector; /// auxiliary vector
67+
68+
public:
69+
/// @name Setters
70+
/// @{
71+
void setPageSize(const std::uint64_t& size);
72+
void addFileMapEntry(const FileMapEntry& entry);
73+
void addAuxVectorEntry(const AuxVectorEntry& entry);
74+
void addPrStatusInfo(const PrStatusInfo& info);
75+
void setAppName(const std::string& name);
76+
void setCmdLine(const std::string& line);
77+
/// @}
78+
79+
/// @name Getters
80+
/// @{
81+
std::uint64_t getPageSize() const;
82+
const std::vector<FileMapEntry>& getFileMap() const;
83+
const std::vector<AuxVectorEntry>& getAuxVector() const;
84+
/// @}
85+
86+
/// @name Helper methods
87+
/// @{
88+
void dump(std::ostream& outStream);
89+
/// @}
90+
};
91+
92+
} // namespace fileformat
93+
} // namespace retdec
94+
95+
#endif
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* @file include/retdec/fileformat/types/note_section/elf_note.cpp
3+
* @brief Class for ELF note section (segment).
4+
* @copyright (c) 2017 Avast Software, licensed under the MIT license
5+
*/
6+
7+
#ifndef RETDEC_FILEFORMAT_TYPES_NOTE_SECTION_ELF_NOTE_H
8+
#define RETDEC_FILEFORMAT_TYPES_NOTE_SECTION_ELF_NOTE_H
9+
10+
#include <string>
11+
#include <vector>
12+
13+
#include "retdec/fileformat/types/sec_seg/sec_seg.h"
14+
15+
namespace retdec {
16+
namespace fileformat {
17+
18+
/**
19+
* Class for one ELF note section or segment entry
20+
*/
21+
class ElfNoteEntry
22+
{
23+
public:
24+
std::string name; ///< interpreted name (owner)
25+
std::size_t type; ///< owner specific type
26+
27+
// Type must be combined with name to tell how to interpret data
28+
29+
std::size_t dataOffset; ///< file offset of note data
30+
std::size_t dataLength; ///< length of note data
31+
32+
/// @name Query methods
33+
/// @{
34+
bool isEmptyNote() const;
35+
/// @}
36+
};
37+
38+
/**
39+
* Class describing one ELF note section or segment
40+
*/
41+
class ElfNoteSecSeg
42+
{
43+
private:
44+
const SecSeg* secSeg; ///< associated section or segment
45+
std::vector<ElfNoteEntry> notes; ///< notes in segment or section
46+
47+
bool malformed = false; ///< set to @c true if notes are malformed
48+
std::string error; ///< possible error message
49+
50+
public:
51+
/// @name Constructors and destructor
52+
/// @{
53+
ElfNoteSecSeg(const SecSeg* assocSecSeg);
54+
~ElfNoteSecSeg();
55+
/// @}
56+
57+
/// @name Setters
58+
/// @{
59+
void setMalformed(const std::string& message = "corrupted note");
60+
/// @}
61+
62+
/// @name Add notes
63+
/// @{
64+
void addNote(ElfNoteEntry&& note);
65+
void addNote(const ElfNoteEntry& note);
66+
/// @}
67+
68+
/// @name Getters
69+
/// @{
70+
std::vector<ElfNoteEntry> getNotes() const;
71+
std::string getErrorMessage() const;
72+
std::size_t getSecSegOffset() const;
73+
std::size_t getSecSegLength() const;
74+
std::string getSectionName() const;
75+
/// @}
76+
77+
/// @name Query methods
78+
/// @{
79+
bool isNamedSection() const;
80+
bool isMalformed() const;
81+
bool isEmpty() const;
82+
/// @}
83+
};
84+
85+
86+
87+
} // namespace fileformat
88+
} // namespace retdec
89+
90+
#endif

src/fileformat/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ set(FILEFORMAT_SOURCES
4949
types/dynamic_table/dynamic_entry.cpp
5050
types/dynamic_table/dynamic_table.cpp
5151
types/strings/string.cpp
52+
types/note_section/elf_notes.cpp
53+
types/note_section/elf_core.cpp
5254
file_format/pe/pe_format_parser/pe_format_parser64.cpp
5355
file_format/pe/pe_format_parser/pe_format_parser.cpp
5456
file_format/pe/pe_format_parser/pe_format_parser32.cpp

0 commit comments

Comments
 (0)