RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
Linker::Section Class Reference

A section of data as read from an object file. More...

#include <section.h>

Inheritance diagram for Linker::Section:
Linker::Buffer Linker::Writable

Public Types

enum  section_flags {
  Readable = 1 << 0 , Writable = 1 << 1 , Execable = 1 << 2 , Mergeable = 1 << 3 ,
  ZeroFilled = 1 << 4 , Fixed = 1 << 5 , Resource = 1 << 6 , Optional = 1 << 7 ,
  Stack = 1 << 8 , Heap = 1 << 9 , CustomFlag = 1 << 10
}
 Possible flags for the section. More...
 

Public Member Functions

 Section (std::string name, int flags=Readable)
 
void SetFlag (unsigned newflags)
 Sets the flags of the section.
 
unsigned GetFlags () const
 
bool IsReadable () const
 
void SetReadable (bool state)
 
bool IsWritable () const
 
void SetWritable (bool state)
 
bool IsExecable () const
 
void SetExecable (bool state)
 
bool IsMergeable () const
 
void SetMergeable (bool state)
 
bool IsFixed () const
 
bool IsZeroFilled () const
 
offset_t SetZeroFilled (bool is_zero_filled)
 
offset_t GetAlign () const
 
void SetAlign (offset_t new_align)
 
offset_t GetStartAddress () const
 
offset_t SetAddress (offset_t new_address)
 For non-fixed segments, sets the starting address and makes the fixed.
 
void ResetAddress (offset_t new_address)
 Forcibly alters the starting address.
 
offset_t Size () const
 
offset_t Expand (offset_t new_size)
 Increases the size of the section by the specified amount.
 
offset_t RealignEnd (offset_t align)
 Expands the section to a size such that its end is at a specified alignment.
 
int GetByte (offset_t offset) override
 Retrieve byte at a certain offset (optional, might not be defined)
 
uint64_t ReadUnsigned (size_t bytes, offset_t offset, EndianType endiantype) const
 
uint64_t ReadUnsigned (size_t bytes, offset_t offset) const
 
int64_t ReadSigned (size_t bytes, offset_t offset, EndianType endiantype) const
 
uint64_t ReadSigned (size_t bytes, offset_t offset) const
 
void WriteWord (size_t bytes, offset_t offset, uint64_t value, EndianType endiantype)
 
void WriteWord (size_t bytes, offset_t offset, uint64_t value)
 
void WriteWord (size_t bytes, uint64_t value, EndianType endiantype)
 Writes value at the current end of the section.
 
void WriteWord (size_t bytes, uint64_t value)
 Writes value at the current end of the section.
 
offset_t Append (const void *new_data, size_t length)
 Appends data at the end of a section.
 
offset_t Append (const char *new_data)
 Appends data at the end of a section.
 
offset_t Append (const Section &other)
 Appends (or merges) another section.
 
offset_t Append (Buffer &buffer)
 Appends (or merges) another writable buffer.
 
Position Start () const
 Retrieves the address of the first byte of the section.
 
Position Base () const
 Retrieves the address of the start of the segment of the section.
 
void ReadFile (std::istream &in)
 Overwrites section data with contents of input stream.
 
void ReadFile (Reader &in)
 Overwrites section data with contents of input stream.
 
offset_t WriteFile (std::ostream &out, offset_t size, offset_t offset=0) const
 Writes data into file.
 
offset_t WriteFile (std::ostream &out) const
 Writes data into file.
 
void Reset ()
 Clear the section.
 
- Public Member Functions inherited from Linker::Buffer
 Buffer (size_t size)
 
offset_t ActualDataSize () override
 Retrieves size of stored data.
 
void ReadFile (Reader &rd)
 
void ReadFile (Reader &rd, offset_t count)
 
offset_t WriteFile (Writer &wr, offset_t count, offset_t offset=0) override
 Writes data of non-zero filled sections.
 
virtual offset_t WriteFile (Writer &wr, offset_t count, offset_t offset=0)=0
 Writes data of non-zero filled sections.
 
offset_t WriteFile (Writer &wr)
 Writes data of non-zero filled sections.
 
- Public Member Functions inherited from Linker::Writable
offset_t WriteFile (Writer &wr)
 Writes data of non-zero filled sections.
 

Static Public Member Functions

static std::shared_ptr< SectionReadFromFile (Reader &rd, std::string name, int flags=Readable)
 
static std::shared_ptr< SectionReadFromFile (Reader &rd, offset_t count, std::string name, int flags=Readable)
 
- Static Public Member Functions inherited from Linker::Buffer
static std::shared_ptr< BufferReadFromFile (Reader &rd)
 
static std::shared_ptr< BufferReadFromFile (Reader &rd, offset_t count)
 

Public Attributes

std::string name
 Name of the section.
 
section_flags flags
 The type of the section.
 
offset_t bias = 0
 Difference between the first byte of the section and the zero address associated with the section.
 
std::variant< std::string, uint16_t > resource_type = " "
 The resource type and ID for a resource section.
 
std::variant< std::string, uint16_t > resource_id = uint16_t(0)
 
std::weak_ptr< Segmentsegment
 The segment a section belongs to.
 
std::string collection_name
 Section name that collects sections.
 

Additional Inherited Members

- Protected Attributes inherited from Linker::Buffer
std::vector< uint8_t > data
 

Detailed Description

A section of data as read from an object file.

A section represents a series of bytes within an object file, executable or memory image. For segmented architectures such as the 8086, it also represents a segment group at the final stages of linking (once all similar sections have been combined), where each symbol belonging to the same segment will have the same preferred segment base.

Member Enumeration Documentation

◆ section_flags

Possible flags for the section.

Enumerator
Readable 

The data in the section can be read at runtime.

Writable 

The section can be written to at runtime.

Execable 

The section data can be used as instruction.

Mergeable 

Sections of the same name with this flag are overlayed instead of appended.

When the sections are zero filled, merged sections will have a size that is the maximum of the merged sections instead of their sum.

For sections with data, only the longer one is kept. They are assumed to have identical data.

TODO: unimplemented

ZeroFilled 

Section is filled with zeros.

Most formats do not require sections that are zero filled to be stored in the binary image. We keep track of which sections are zero filled using this flag.

Fixed 

Section resides at a fixed address and cannot be moved.

When generating code, all sections are assigned starting addresses and are converted to fixed sections.

Resource 

Section data represents a resource that has to be handled differently.

The NE, LE/LX, PE and classic Macintosh file formats support storing resources within their binary images. To ease usage, these resources can be stored in the object file instead of requiring a separate resource file.

Optional 

Section data may be unallocated if necessary.

Reserved for file formats such as MZFormat and CPM86Format where additional memory can be requested.

Stack 

Stack section.

Heap 

Heap section.

CustomFlag 

Other flags.

Member Function Documentation

◆ Base()

Position Section::Base ( ) const

Retrieves the address of the start of the segment of the section.

For Intel 8086, it is expected that the data in a segment does not begin at the same location as the base of the segment.

◆ Expand()

offset_t Section::Expand ( offset_t new_size)

Increases the size of the section by the specified amount.

Parameters
new_sizeThe new size for the section. If it is smaller than the current size, nothing is changed.
Returns
The actual amount of bytes the section was increased by.

◆ GetByte()

int Section::GetByte ( offset_t offset)
overridevirtual

Retrieve byte at a certain offset (optional, might not be defined)

Reimplemented from Linker::Buffer.

◆ ReadFile() [1/2]

void Section::ReadFile ( Reader & in)

Overwrites section data with contents of input stream.

Note that only as many bytes are read in as the size of the section.

◆ ReadFile() [2/2]

void Section::ReadFile ( std::istream & in)

Overwrites section data with contents of input stream.

Note that only as many bytes are read in as the size of the section.

◆ SetAddress()

offset_t Section::SetAddress ( offset_t new_address)

For non-fixed segments, sets the starting address and makes the fixed.

If the segment is already fixed, the address is not changed. Alignment requirements might cause the new address to be incremented.

Parameters
new_addressAttempted starting address
Returns
The new starting address

◆ SetFlag()

void Section::SetFlag ( unsigned newflags)

Sets the flags of the section.

Certain flags require special handling.

◆ WriteFile() [1/2]

offset_t Section::WriteFile ( std::ostream & out) const

Writes data into file.

Note that zero filled sections do not write anything.

Parameters
outOutput stream
Returns
Count of actual number of bytes written

◆ WriteFile() [2/2]

offset_t Section::WriteFile ( std::ostream & out,
offset_t size,
offset_t offset = 0 ) const

Writes data into file.

Note that zero filled sections do not write anything.

Parameters
outOutput stream
sizeMaximum number of bytes to write
Returns
Count of actual number of bytes written

Member Data Documentation

◆ bias

offset_t Linker::Section::bias = 0

Difference between the first byte of the section and the zero address associated with the section.

On the Intel 8086, addresses consist of two parts: a segment address and an offset within that segment. The linker collects sections and assigns them to segments, which often map to hardware segments. On some platforms, the address of the first byte of a section will not be the same as the address of the beginning of the hardware segment it belongs to. The bias field stores this difference.

For example, the first byte of a .com file will have an address 0x100. This is represented by setting the bias to -0x100.

◆ resource_type

std::variant<std::string, uint16_t> Linker::Section::resource_type = " "

The resource type and ID for a resource section.

All resources can have a resource type and a resource ID that can usually be a string or an integer. Some platforms specify which one is which (for example, Macintosh resources have a 4-character type but a 16-bit ID). For others (for example, the NE format), they can be either.

◆ segment

std::weak_ptr<Segment> Linker::Section::segment

The segment a section belongs to.

This field is assigned as part of the linking process.


The documentation for this class was generated from the following files: