Skip to content

Obtaining sections by section numbers is insane, and symbols return other number than they are contained in. #16

@MajsterTynek

Description

@MajsterTynek
#include <coffi/coffi.hpp>
using namespace COFFI;

void symprint(symbol *s) {
    std::cout << s->get_index() << '\t'
        << s->get_section_number() << '\t'
        << s->get_name() << std::endl;
}

void secprint(section *sec) {
    std::cout << sec->get_index() << '\t'
        << sec->get_name() << std::endl;
}

int main()
{
    //auto fraw = std::ios::binary | std::ios::in;
    //std::ifstream obj("./st_obj/ioinit.obj", fraw);
    
    coffi c;
    if (!c.load("./st_obj/ioinit.obj"))
        return 1;

    for (auto sym : *c.get_symbols())
        symprint(&sym);

    std::cout << std::endl;
    for (auto sec : c.get_sections())
        secprint(sec);

    std::cout << std::endl;
    auto s = c.get_symbol("___pioinfo");
    auto sn = s->get_section_number();
    section *shard = c.get_sections()[sn-1];

    symprint(s);
    //secprint(shard); // SIGSEV -1073741819
    return 0;
}
0       65534   .file
2       65535   @comp.id
3       1       .data
5       1       ___badioinfo
6       0       __nhandle
7       0       ___pioinfo
8       2       .text
10      2       __ioinit
11      0       __imp__SetHandleCount@4
12      0       __imp__GetStdHandle@4
13      0       __imp__GetFileType@4
14      0       __imp__GetStartupInfoA@4
15      0       __amsg_exit
16      0       _malloc
17      3       .debug$F
19      4       .text
21      4       __ioterm
22      0       _free
23      5       .debug$F

0       .data
1       .text
2       .debug$F
3       .text
4       .debug$F

7       0       ___pioinfo

There is no sensible way to iterate over sections container,
also accesing section fields returns garbage on latest commits.
Could be more intuitive; aslo I forgot to set binary mode for file stream.

Generally sections[section_number - 1] did work for me,
but then I have noticed symbols return 0 for unknown reasons,
and that causes SIGSEV on computed index -1 (offset).

I will revert back to 1.1 and try overwriting symbols' section numbers manually.
In comparison, Relocation Entries seem to be right with section number values.


This is my current temporary workaround that works:

    // workaround for borken section numbers
    int secnum = 1;
    for (auto &sym : *c.get_symbols())
    {
        unsigned int v = sym.get_value();
        int sn = (int16_t)sym.get_section_number();
        int sc = (int8_t)sym.get_storage_class();
        
        if ( sc == 3 && !v) 
            secnum = sn; // a new section name
        if (v && sn == 0)
            sn = secnum; // a defined symbol
        sym.set_section_number(sn);
    }
0       65534   .file
2       65535   @comp.id
3       1       .data
5       1       ___badioinfo
6       1       __nhandle
7       1       ___pioinfo
8       2       .text
10      2       __ioinit
11      0       __imp__SetHandleCount@4
12      0       __imp__GetStdHandle@4
13      0       __imp__GetFileType@4
14      0       __imp__GetStartupInfoA@4
15      0       __amsg_exit
16      0       _malloc
17      3       .debug$F
19      4       .text
21      4       __ioterm
22      0       _free
23      5       .debug$F

EDIT: Took care of missed undefined symbols.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions