Skip to content

Commit 0fc236f

Browse files
filipnavaraxoofx
authored andcommitted
Add support for >= 0xff00 sections
1 parent f516c4d commit 0fc236f

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

src/LibObjectFile/Elf/ElfObjectFile.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ public unsafe void UpdateLayout(DiagnosticBag diagnostics)
230230
var section = sections[i];
231231
if (i == 0 && section.Type == ElfSectionType.Null)
232232
{
233+
section.Size = VisibleSectionCount >= ElfNative.SHN_LORESERVE ? VisibleSectionCount : 0;
234+
section.Link = (SectionHeaderStringTable?.SectionIndex ?? 0) >= ElfNative.SHN_LORESERVE ? SectionHeaderStringTable : null;
233235
continue;
234236
}
235237

src/LibObjectFile/Elf/ElfWriter{TEncoder}.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,9 @@ private unsafe void WriteSectionHeader32()
155155
// entries for sections
156156
_encoder.Encode(out hdr.e_shoff, (uint)Layout.OffsetOfSectionHeaderTable);
157157
_encoder.Encode(out hdr.e_shentsize, Layout.SizeOfSectionHeaderEntry);
158-
_encoder.Encode(out hdr.e_shnum, (ushort)ObjectFile.VisibleSectionCount);
159-
_encoder.Encode(out hdr.e_shstrndx, (ushort)(ObjectFile.SectionHeaderStringTable?.SectionIndex ?? (ushort)0));
158+
_encoder.Encode(out hdr.e_shnum, ObjectFile.VisibleSectionCount >= ElfNative.SHN_LORESERVE ? (ushort)0 : (ushort)ObjectFile.VisibleSectionCount);
159+
uint shstrSectionIndex = ObjectFile.SectionHeaderStringTable?.SectionIndex ?? 0u;
160+
_encoder.Encode(out hdr.e_shstrndx, shstrSectionIndex >= ElfNative.SHN_LORESERVE ? (ushort)ElfNative.SHN_XINDEX : (ushort)shstrSectionIndex);
160161

161162
Write(hdr);
162163
}
@@ -181,11 +182,13 @@ private unsafe void WriteSectionHeader64()
181182
// entries for sections
182183
_encoder.Encode(out hdr.e_shoff, Layout.OffsetOfSectionHeaderTable);
183184
_encoder.Encode(out hdr.e_shentsize, (ushort)sizeof(Elf64_Shdr));
184-
_encoder.Encode(out hdr.e_shnum, (ushort)ObjectFile.VisibleSectionCount);
185-
_encoder.Encode(out hdr.e_shstrndx, (ushort)(ObjectFile.SectionHeaderStringTable?.SectionIndex ?? (ushort)0));
185+
_encoder.Encode(out hdr.e_shnum, ObjectFile.VisibleSectionCount >= ElfNative.SHN_LORESERVE ? (ushort)0 : (ushort)ObjectFile.VisibleSectionCount);
186+
uint shstrSectionIndex = ObjectFile.SectionHeaderStringTable?.SectionIndex ?? 0u;
187+
_encoder.Encode(out hdr.e_shstrndx, shstrSectionIndex >= ElfNative.SHN_LORESERVE ? (ushort)ElfNative.SHN_XINDEX : (ushort)shstrSectionIndex);
186188

187189
Write(hdr);
188190
}
191+
189192
private void CheckProgramHeaders()
190193
{
191194
if (ObjectFile.Segments.Count == 0)

src/LibObjectFile/Elf/Sections/ElfBinarySection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public override ElfSectionType Type
4242
}
4343

4444
public override ulong TableEntrySize => OriginalTableEntrySize;
45-
45+
4646
/// <summary>
4747
/// Gets or sets the associated stream to this section.
4848
/// </summary>

src/LibObjectFile/Elf/Sections/ElfNullSection.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,29 @@ public override void Verify(DiagnosticBag diagnostics)
2020
!Name.IsEmpty ||
2121
VirtualAddress != 0 ||
2222
Alignment != 0 ||
23-
!Link.IsEmpty ||
2423
!Info.IsEmpty ||
25-
Offset != 0 ||
26-
Size != 0)
24+
Offset != 0)
2725
{
2826
diagnostics.Error(DiagnosticId.ELF_ERR_InvalidNullSection, "Invalid Null section. This section should not be modified and all properties must be null");
2927
}
28+
29+
if (Size != 0 && Parent.VisibleSectionCount < ElfNative.SHN_LORESERVE)
30+
{
31+
diagnostics.Error(DiagnosticId.ELF_ERR_InvalidNullSection, "Invalid Null section. Size is non-zero but number of sections is lower than SHN_LORESERVE");
32+
}
33+
else if (Size == 0 && Parent.VisibleSectionCount >= ElfNative.SHN_LORESERVE)
34+
{
35+
diagnostics.Error(DiagnosticId.ELF_ERR_InvalidNullSection, "Invalid Null section. Size is zero but number of sections is higher or equal to SHN_LORESERVE");
36+
}
37+
38+
if (!Link.IsEmpty && (Parent.SectionHeaderStringTable?.SectionIndex ?? 0) < ElfNative.SHN_LORESERVE)
39+
{
40+
diagnostics.Error(DiagnosticId.ELF_ERR_InvalidNullSection, "Invalid Null section. Link is non-zero but index of section header string section is lower than SHN_LORESERVE");
41+
}
42+
else if (Link.IsEmpty && (Parent.SectionHeaderStringTable?.SectionIndex ?? 0) >= ElfNative.SHN_LORESERVE)
43+
{
44+
diagnostics.Error(DiagnosticId.ELF_ERR_InvalidNullSection, "Invalid Null section. Link is non-zero but index of section header string section is higher or equal to SHN_LORESERVE");
45+
}
3046
}
3147

3248
public override void UpdateLayout(DiagnosticBag diagnostics)

0 commit comments

Comments
 (0)