Skip to content

Michael elf parsing - LSDA parsing #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

Mchan2003
Copy link
Collaborator

  • Added ElfParser class
  • Features
    • able to parse out the headers
    • open and close ELF files
    • implemented LSDA parsing

@Mchan2003 Mchan2003 linked an issue May 1, 2025 that may be closed by this pull request
@Mchan2003 Mchan2003 requested a review from kammce May 1, 2025 20:20
…he elf header

Completed Section and Program header parsing

implement function to store lsda into a binary file and another to close the elf file

fixed formatting

fixed syntax errors
@Mchan2003 Mchan2003 force-pushed the Michael-ELF_parsing branch from 3f85911 to e9790c5 Compare May 1, 2025 21:09
CMakeLists.txt Outdated
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_custom_target(copy_compile_commands ALL
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_BINARY_DIR}/compile_commands.json
${CMAKE_SOURCE_DIR}/compile_commands.json
DEPENDS ${CMAKE_BINARY_DIR}/compile_commands.json)

# Find package dependancies
list(APPEND CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/build/Debug/generators") #TODO: Remove due to this being a temporary patch for libraries to work
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try and keep code at 80 columns. Makes it easier to look at in most editors.

Comment on lines 1 to 11
#pragma once
#include <bitset>
#include <err.h>
#include <fcntl.h>
#include <gelf.h>
#include <libelf.h>
#include <print>
#include <stdlib.h>
#include <string>
#include <sysexits.h>
#include <unistd.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#pragma once
#include <bitset>
#include <err.h>
#include <fcntl.h>
#include <gelf.h>
#include <libelf.h>
#include <print>
#include <stdlib.h>
#include <string>
#include <sysexits.h>
#include <unistd.h>
#pragma once
#include <err.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sysexits.h>
#include <unistd.h>
#include <gelf.h>
#include <libelf.h>
#include <bitset>
#include <string>
#include <print>

Lets follow the libhal style guide for headers: https://libhal.github.io/4.1/contributor_guide/style/#s143-include-order

Comment on lines 16 to 17
ElfParser(char** t_file_ptr)
: m_file(t_file_ptr){};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ElfParser(char** t_file_ptr)
: m_file(t_file_ptr){};
ElfParser(char** p_file_ptr)
: m_file(p_file_ptr){};

Input parameters should have prefix p_

int m_fd, m_fbin;
size_t m_n, m_shstrndx;

char** m_file;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend replacing this with std::string so this object owns the string data.

Comment on lines 27 to 41
int m_elf_class{ 0 };
int m_fd, m_fbin;
size_t m_n, m_shstrndx;

char** m_file;

char* m_name;
Elf_Scn* m_scn;
Elf_Data* m_data;

Elf* m_e;
GElf_Ehdr m_ehdr;
GElf_Phdr m_phdr;
GElf_Shdr m_shdr;
//Gelf_Sym m_sym;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot, from visual inspection understand what of these are and why they need to be in the fields of the class.

}
}

void ElfParser::getSection(std::string section)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the API name makes sense for what it does. Its more like it dumps the section to a bin file.

Comment on lines 187 to 191
void ElfParser::closeElf()
{
close(m_fd);
std::println("ELF file closed");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in the destructor.

src/main.cpp Outdated
Comment on lines 1 to 4
#include "../include/elf_parser.hpp"
#include <bitset>
#include <print>
#include <string>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the order

src/main.cpp Outdated
Comment on lines 8 to 9
std::string lsda = ".gcc_except_table";
std::string symtab = ".symtab";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to not use std::string here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::string_view would work here instead.

src/main.cpp Outdated
return 0;
;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add newline to file for all files.

@kammce
Copy link
Member

kammce commented May 1, 2025

Also make everything snake_case

…, move loading of the headers to the constructor, reformat to fit libhal formatting standard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ELF Parsing
2 participants