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

Add support for generating uninitialized sections #306

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 22 additions & 8 deletions llvm/tools/objwriter/objwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,16 @@ MCSection *ObjectWriter::GetSpecificSection(const char *SectionName,
const char *ComdatName) {
Triple TheTriple(TripleName);
MCSection *Section = nullptr;
SectionKind Kind = (attributes & CustomSectionAttributes_Executable)
? SectionKind::getText()
: (attributes & CustomSectionAttributes_Writeable)
? SectionKind::getData()
: SectionKind::getReadOnly();
SectionKind Kind;
if (attributes & CustomSectionAttributes_Executable)
Kind = SectionKind::getText();
else if (attributes & CustomSectionAttributes_Uninitialized)
Kind = SectionKind::getBSS();
else if (attributes & CustomSectionAttributes_Writeable)
Kind = SectionKind::getData();
else
Kind = SectionKind::getReadOnly();

switch (TheTriple.getObjectFormat()) {
case Triple::MachO: {
unsigned typeAndAttributes = 0;
Expand All @@ -260,6 +265,9 @@ MCSection *ObjectWriter::GetSpecificSection(const char *SectionName,
// boundaries.
typeAndAttributes |= MachO::S_ATTR_PURE_INSTRUCTIONS;
}
if (attributes & CustomSectionAttributes_Uninitialized) {
typeAndAttributes |= MachO::S_ZEROFILL;
Copy link
Member

Choose a reason for hiding this comment

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

Is zerofill needed for correctness or just MachO wants that for uninitialized data?

Copy link
Member Author

@MichalStrehovsky MichalStrehovsky Nov 7, 2022

Choose a reason for hiding this comment

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

Without that we were generating a lot of zeros in the object file I generated.

It does the same thing as COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA for PE and ELF::SHT_NOBITS for ELF below. But yeah, I don't know why LLVM has both these format specific enums and SectionKind above.

I haven't actually tried end-to-end on mac, but the object file looks about right now in llvm-objdump.

Copy link
Member

Choose a reason for hiding this comment

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

I see. I am not familiar with MachO at this level. I guess it needs to fill the space with zeroes one way or another. So it is either zeroes in the obj file or a fill it with zeroes at load/demand time. The flag could be selecting that.
Makes sense.

}
Section = OutContext->getMachOSection(
(attributes & CustomSectionAttributes_Executable) ? "__TEXT" : "__DATA",
SectionName, typeAndAttributes, Kind);
Expand All @@ -271,8 +279,11 @@ MCSection *ObjectWriter::GetSpecificSection(const char *SectionName,
if (attributes & CustomSectionAttributes_Executable) {
Characteristics |= COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE;
} else if (attributes & CustomSectionAttributes_Writeable) {
Characteristics |=
COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_WRITE;
Characteristics |= COFF::IMAGE_SCN_MEM_WRITE;
if (attributes & CustomSectionAttributes_Uninitialized)
Characteristics |= COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
else
Characteristics |= COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
} else {
Characteristics |= COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
}
Expand All @@ -299,8 +310,11 @@ MCSection *ObjectWriter::GetSpecificSection(const char *SectionName,
} else if (attributes & CustomSectionAttributes_Writeable) {
Flags |= ELF::SHF_WRITE;
}
unsigned SectionType = (attributes & CustomSectionAttributes_Uninitialized)
? ELF::SHT_NOBITS
Copy link
Member

Choose a reason for hiding this comment

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

for BSS section the SHT_NOBITS should be combined with SHF_ALLOC, to be present in memory, which we get from line 301 above and SHF_WRITE, which we get because we set the writeable attribute in object writer at:

              case SectionType.Uninitialized:
                    attributes |= CustomSectionAttributes.Uninitialized | CustomSectionAttributes.Writeable;
                    break;

Looks right to me.

: ELF::SHT_PROGBITS;
Section =
OutContext->getELFSection(SectionName, ELF::SHT_PROGBITS, Flags, 0,
OutContext->getELFSection(SectionName, SectionType, Flags, 0,
ComdatName != nullptr ? ComdatName : "");
break;
}
Expand Down
1 change: 1 addition & 0 deletions llvm/tools/objwriter/objwriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ enum CustomSectionAttributes : int32_t {
CustomSectionAttributes_ReadOnly = 0x0000,
CustomSectionAttributes_Writeable = 0x0001,
CustomSectionAttributes_Executable = 0x0002,
CustomSectionAttributes_Uninitialized = 0x0004,
CustomSectionAttributes_MachO_Init_Func_Pointers = 0x0100,
};

Expand Down