473,385 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Stuffing bytes into structs and endians

I'm trying to parse an ELF file to build a human readable representation. I
know it's been done before, and there are tools such as objdump, nm,
readelf, c++filt, etc. I can look at the source to see how they are
implemented. In most cases, that means reading C, not C++. I do have an
example of C++ code that works. I thought it might be a good idea to take
a different approach than that code takes.

Rather than processing the data from in std::ifstream as ELFIO does, I
wanted to read it into a buffer, and work on it from there. One option is
to use a std::stringstream, and treat it like and do basically the same
thing ELFIO does with the std::ifstream. What I currently have is a
std::vector<char>, which I thought would be a good thing to use. But if I
try to do that, I have to find a different way to do things such as

m_pStream->read( reinterpret_cast<char*>( &m_header ), sizeof( m_header ) );

My intent was to work on the file from the point of view of it just being
data, rather than a stream. Is that simply "not the way it's done"?
Another question I have is this: The ELF file starts with an identification
tag which carries such information as encoding type (big endian, little
endian), the class of the machine(32 or 64-bit), etc. That's all in
specifically ordered bytes. That leaves a question opened. Doesn't the
host encoding determine which byte is treated as 0? IOW, the first four
bytes are specified as '0x7f', 'E', 'L', 'F'. If that were read by a
machine that uses the opposite endian, would that not come across as
'F','L','E','0x7f'? Even more perplexing is this: the rest of the ELF
header is formed of heterogeneous data types. See below. Does that mean
some of the data might be put into the wrong location by the
reinterpret_cast<> above?

// Platform specific definitions.
typedef unsigned long Elf32_Addr;
typedef unsigned short Elf32_Half;
typedef unsigned long Elf32_Off;
typedef signed long Elf32_Sword;
typedef unsigned long Elf32_Word;
// ELF file header
struct Elf32_Ehdr {
unsigned char e_ident[EI_NIDENT];
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff;
Elf32_Word e_flags;
Elf32_Half e_ehsize;
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize;
Elf32_Half e_shnum;
Elf32_Half e_shstrndx;
};
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Aug 5 '05 #1
2 1955
The following article should answer your questions about byte alignment
and ordering:

http://www.eventhelix.com/RealtimeMa...ndOrdering.htm

--
EventStudio 2.5 - http://www.EventHelix.com/EventStudio
Generate Sequence diagrams from a simple declarative language

Aug 6 '05 #2
EventHelix.com wrote:
The following article should answer your questions about byte alignment
and ordering:

http://www.eventhelix.com/RealtimeMa...ndOrdering.htm


OK, now I know what my mistake was. The only ordering significance has to
do the how the individual bytes are interpreted as part of a larger number.
Byte [0] is still byte [0] no matter what endian I have. So byte [5] will
always contain the data encoding byte that tells me which endian to use.
Thanks.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Aug 6 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: | last post by:
Assume that we have the following structure to communicate over a serial communication device (Rs232) or ethernet between two or many computers. There is no guaranty that computers have the same...
4
by: James Roberge | last post by:
I am having a little trouble getting my union/struct to work correctly. I am creating a struct that will contain information about the status of various Z80 cpu registers in an emulator i am...
45
by: Curt Geske | last post by:
I'm suprised no one suggested a union! #include <stdio.h> union _x { long lng; char byt; } X; void main( void )
19
by: Lorenzo J. Lucchini | last post by:
My code contains this declaration: : typedef union { : word Word; : struct { : byte Low; : byte High; : } Bytes; : } reg;
4
by: Michael Yanowitz | last post by:
Hello: For some reason I can't figure out how to split a 4-byte (for instance) float number (such as 3.14159265359) into its 4-bytes so I can send it via a socket to another computer. For...
5
by: Bo Berglund | last post by:
Preamble: I am a Delphi (Pascal) programmer who has stumbled into the C++ world by necessity since Borland has no 64 bit compiler yet out.... I used to do a lot of ANSI C programming for embedded...
14
by: Francine.Neary | last post by:
Consider the following situation. I have a large static array s of structs, say of size 500. I also need a smaller array of chars, say of size 100, which has nothing to do with the struct. To...
1
by: manojdagar | last post by:
here now stands that small knot of villages known as the Endians, a mighty forest once stood. Indeed, legand has it that you could have stoodon the edge of the wood and seen it stretch out for miles,...
8
by: sexauthor | last post by:
I'm converting a VB6 application over that called a 3rd party DLL with specific data structures. The VB6 code defined custom types for those data structures (ie: one with the specific data types,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.