Skip to content
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

Added various features #655

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion tools/cxbe/Exe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Exe::Exe(const char *x_szFilename)
// read section headers
{
m_SectionHeader = new SectionHeader[m_Header.m_sections];
m_SectionHeader_longname = new SectionHeader_longname[m_Header.m_sections];

printf("Exe::Exe: Reading Section Headers...\n");

Expand All @@ -115,7 +116,46 @@ Exe::Exe(const char *x_szFilename)
goto cleanup;
}

printf("OK %d\n", v);
// interpret long section names
if(m_SectionHeader[v].m_name[0] == '/')
Copy link
Member

Choose a reason for hiding this comment

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

How was this commit tested?
In all my testing, I could not get lld-link to produce a section that exceeds 8 characters, even with trickery. This matches Microsoft's documentation, which says that executable images do not use a string table and do not support section names longer than 8 characters.

Copy link
Author

@PQCraft PQCraft Sep 30, 2023

Choose a reason for hiding this comment

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

I used objcopy to rename a section I made in some inline asm
XTIMAGE -> $$XTIMAGE

Copy link
Member

Choose a reason for hiding this comment

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

I have to say I'm not a fan, I'd rather stay a mile away from intentionally breaking the PE spec (or giving the impression that we support doing so).

Copy link
Author

@PQCraft PQCraft Sep 30, 2023

Choose a reason for hiding this comment

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

The thing with / is documented in the spec on microsoft's page. While it says that long section names are not supported with executables, objcopy can still produce executables with long section names and they load and run just fine.

Oh also debug sections use long section names

Copy link
Member

Choose a reason for hiding this comment

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

Yes, it is documented as not being valid for executable images. While it may be possible to hack around the linker for that, it is breaking the spec.

Copy link
Author

Choose a reason for hiding this comment

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

Also I added this because I actually need this to have a title image.
It would incorrectly add debug sections and the image sections as something like /43 which ofc would not be picked up by the dashboard as an icon.

Copy link
Member

Choose a reason for hiding this comment

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

  1. How does the XDK do it?
  2. Can't we tell cxbe to add or rename sections? (Not that I'd like the added complexity, but it's probably better than breaking the spec)

{
m_SectionHeader_longname[v].m_offset = 0;
for(uint32 i = 1; i < 8; ++i)
{
char c = m_SectionHeader[v].m_name[i];
if(!c)
break;
if(c < '0' || c > '9') // not a long section after all?
goto notlong;
m_SectionHeader_longname[v].m_offset *= 10;
m_SectionHeader_longname[v].m_offset += c - '0';
}
m_SectionHeader_longname[v].m_longname = new char[256]();

long tmppos = ftell(ExeFile);
fseek(ExeFile, m_Header.m_symbol_table_addr + m_SectionHeader_longname[v].m_offset,
SEEK_SET);

uint32 i;
for(i = 0; i < 255; ++i)
{
int c = fgetc(ExeFile);
if(!c || c == EOF)
break;
m_SectionHeader_longname[v].m_longname[i] = c;
}
m_SectionHeader_longname[v].m_longname[i] = 0;

fseek(ExeFile, tmppos, SEEK_SET);
printf("OK %d (long)\n", v, m_SectionHeader_longname[v].m_offset,
m_SectionHeader_longname[v].m_longname);
}
else
{
notlong:;
m_SectionHeader_longname[v].m_longname = NULL;
printf("OK %d\n", v);
}
}
}

Expand Down Expand Up @@ -184,6 +224,7 @@ Exe::Exe(const char *x_szFilename)
void Exe::ConstructorInit()
{
m_SectionHeader = NULL;
m_SectionHeader_longname = NULL;
m_bzSection = NULL;
}

Expand All @@ -193,12 +234,16 @@ Exe::~Exe()
if(m_bzSection != 0)
{
for(uint32 v = 0; v < m_Header.m_sections; v++)
{
delete[] m_SectionHeader_longname[v].m_longname;
delete[] m_bzSection[v];
}

delete[] m_bzSection;
}

delete[] m_SectionHeader;
delete[] m_SectionHeader_longname;
}

// export to Exe file
Expand Down
7 changes: 7 additions & 0 deletions tools/cxbe/Exe.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ class Exe : public Error
uint32 m_characteristics; // characteristics for this segment
} __attribute((packed)) * m_SectionHeader;

// array to store long section names
struct SectionHeader_longname
{
char *m_longname;
uint32 m_offset;
} __attribute((packed)) * m_SectionHeader_longname;

// array of section data
uint08 **m_bzSection;

Expand Down
31 changes: 25 additions & 6 deletions tools/cxbe/Xbe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,16 @@ Xbe::Xbe(class Exe *x_Exe, const char *x_szTitle, bool x_bRetail, const std::vec
{
uint32 s = 0;

while(s < 8 && x_Exe->m_SectionHeader[v].m_name[s] != '\0')
s++;
if(x_Exe->m_SectionHeader_longname[v].m_longname)
{
while(s < 255 && x_Exe->m_SectionHeader_longname[v].m_longname[s] != '\0')
s++;
}
else
{
while(s < 8 && x_Exe->m_SectionHeader[v].m_name[s] != '\0')
s++;
}

mrc += s + 1;
}
Expand Down Expand Up @@ -345,7 +353,7 @@ Xbe::Xbe(class Exe *x_Exe, const char *x_szTitle, bool x_bRetail, const std::vec

// write section headers / section names
{
m_szSectionName = new char[m_Header.dwSections][9];
m_szSectionName = new char[m_Header.dwSections][256];

m_SectionHeader = new SectionHeader[m_Header.dwSections];

Expand Down Expand Up @@ -431,10 +439,21 @@ Xbe::Xbe(class Exe *x_Exe, const char *x_szTitle, bool x_bRetail, const std::vec
memset(secn, 0, 8);

m_SectionHeader[v].dwSectionNameAddr = hwc_secn;
while(s < 8 && x_Exe->m_SectionHeader[v].m_name[s] != '\0')
if(x_Exe->m_SectionHeader_longname[v].m_longname)
{
m_szSectionName[v][s] = secn[s] = x_Exe->m_SectionHeader[v].m_name[s];
s++;
while(s < 255 && x_Exe->m_SectionHeader_longname[v].m_longname[s] != '\0')
{
m_szSectionName[v][s] = secn[s] = x_Exe->m_SectionHeader_longname[v].m_longname[s];
s++;
}
}
else
{
while(s < 8 && x_Exe->m_SectionHeader[v].m_name[s] != '\0')
{
m_szSectionName[v][s] = secn[s] = x_Exe->m_SectionHeader[v].m_name[s];
s++;
}
}

m_szSectionName[v][s] = '\0';
Expand Down
4 changes: 2 additions & 2 deletions tools/cxbe/Xbe.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ class Xbe : public Error
uint32 dwCharacteristics; // characteristics
} __attribute((packed)) * m_TLS;

// Xbe section names, each 8 bytes max and null terminated
char (*m_szSectionName)[9];
// Xbe section names, each 255 bytes max and null terminated
char (*m_szSectionName)[256];

// Xbe sections
uint08 **m_bzSection;
Expand Down