Jump to content

  • Log In with Google      Sign In   
  • Create Account


code -> .data & data -> .code


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
22 replies to this topic

#1 fir   Members   -  Reputation: -441

Like
-3Likes
Like

Posted 27 February 2014 - 03:21 PM

I heard that sometimes (often?) compilers put the data to code section

and vice versa (code to data section) though it is generally not good

and 'can even degrade performance'  - but why they do that was not explained

 

does maybe someone know something about that?


Edited by fir, 27 February 2014 - 03:30 PM.


Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 11970

Like
3Likes
Like

Posted 27 February 2014 - 03:23 PM

Other than hearing this, have you also read it somewhere on the interwebs? Perhaps you can post a link to such a claim, so we can know exactly what you are talking about?



#3 Ravyne   Crossbones+   -  Reputation: 6779

Like
1Likes
Like

Posted 27 February 2014 - 03:31 PM

I'm not super knowledgeable in this area, perhaps someone can illuminate me -- aren't the code and data sections mostly throwbacks to the 16bit segmented memory days?

 

I recall that when I learned to program in a compilable version of QuickBASIC (v4.5 for those who remember) they actually had instructions for setting the code and data segments that were active. One of the major optimizations I discovered in one of my programs once, was that the structure of my map rendering was causing me to jump all over memory and even across data segments -- which was obviously horrible. When I fixed it, I went from 5fps to 50fps, in a tight rendering loop where blitting should have been the bottleneck.



#4 fir   Members   -  Reputation: -441

Like
0Likes
Like

Posted 27 February 2014 - 03:37 PM

Other than hearing this, have you also read it somewhere on the interwebs? Perhaps you can post a link to such a claim, so we can know exactly what you are talking about?

alright, the first source is in agner fog objconv instruction

 

"11.27 How does the disassembler distinguish between code and data?
The first assumption is that code segments contain code and data segments contain data.
Unfortunately, some compilers put jump tables and other data into the code segment, even
though this gives inferior performance."
 
the second
 
"The problem wouldn't be as difficult if data were limited to the .data section (segment) of an executable (explained in a later chapter) and if executable code were limited to the .code section of an executable, but this is often not the case. Data may be inserted directly into the code section (e.g. jump address tables, constant strings), and executable code may be stored in the data section (although new systems are working to prevent this for security reasons)."
 
from
 
but this is not further explained and i wonder what is a reason of 
this "violations"


#5 frob   Moderators   -  Reputation: 19010

Like
5Likes
Like

Posted 27 February 2014 - 03:52 PM

Some values must be initialized as non-zero data values. These are stored in the executable in something called a data segment.

Sometimes the value will be used immediately in code rather than loaded up as data. For example, when multiplying by a constant the compiler can just put the multiplication directly into the instruction stream rather than needing to dereference a memory address.

Storing executable instructions in data is not normally done in legitimate programs and is increasingly forbidden by hardware. Don't try it on modern x86 chips or several others; they implement a "data execution prevention" system where segments are marked with an NX (No eXecute) bit, and attempting to run that code in data will immediately crash your program.
Check out my personal indie blog at bryanwagstaff.com.

#6 wack   Members   -  Reputation: 1231

Like
5Likes
Like

Posted 27 February 2014 - 03:52 PM

You can really put anything you want in sections of the executable, it's all just bytes anyway. The sections can be called anything also. The only difference is the flags that the OS uses to allocate the memory. Pure data sections could for example be allocated as non-executable or read-only.

This is the function that Windows uses to allocate memory when it loads executables, it might give you some hints about this.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa366887(v=vs.85).aspx

I'm not sure what he's on about when he says that data in the code section could give you performance issues. The data needs to be somewhere, and I don't see why loading it from one arbitrary memory address would be worse than doing it from another arbitrary address. It might even be beneficial to have it near the code, because the jump table data would likely end up in the same memory page as the code that uses it... But I'm not sure, so don't qote me on that. But the compiler writers are usually not morons, so it would be pretty safe to bet that they know what they are doing.

#7 fir   Members   -  Reputation: -441

Like
0Likes
Like

Posted 27 February 2014 - 04:13 PM

You can really put anything you want in sections of the executable, it's all just bytes anyway. The sections can be called anything also. The only difference is the flags that the OS uses to allocate the memory. Pure data sections could for example be allocated as non-executable or read-only.

This is the function that Windows uses to allocate memory when it loads executables, it might give you some hints about this.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa366887(v=vs.85).aspx

I'm not sure what he's on about when he says that data in the code section could give you performance issues. The data needs to be somewhere, and I don't see why loading it from one arbitrary memory address would be worse than doing it from another arbitrary address. It might even be beneficial to have it near the code, because the jump table data would likely end up in the same memory page as the code that uses it... But I'm not sure, so don't qote me on that. But the compiler writers are usually not morons, so it would be pretty safe to bet that they know what they are doing.

 

not always sometimes compilers do a really weak stuff...

i also sont know what he mean about this performance issues,

though i must say that year ot two years ago i was calling the data - i mean i wrote the procedures right in machine code in my prog like

 

char asmDot_SSE[] =
{
 
 0xC8, 0x00, 0x00, 0x00,
 0x8B, 0x45, 0x08,
 0x8B, 0x5D, 0x0C,
 0x8B, 0x4D, 0x10,
 0x0F, 0x10, 0x00,
 0x0F, 0x10, 0x0B,
 0x0F, 0x59, 0xC1,
 0x0F, 0x12, 0xC8,
 0x0F, 0x58, 0xC8,
 0x0F, 0x28, 0xC1,
 0x0F, 0xC6, 0xC9, 0x01,
 0xF3, 0x0F, 0x58, 0xC1,
 0xF3, 0x0F, 0x11, 0x01,
 0xC9,
 0xC3
 
};
 
and when counting the execution times it was much slower than 
executing it in normal code section - i dont know what was a reason of that but it worked but had a speed penalty sadly
 
mayve some guarding mechanism is involved ? maybe it works also in opossite way, i mean accessing data from .code pages.. dont know

Edited by fir, 27 February 2014 - 04:15 PM.


#8 fir   Members   -  Reputation: -441

Like
0Likes
Like

Posted 27 February 2014 - 04:21 PM

I'm not super knowledgeable in this area, perhaps someone can illuminate me -- aren't the code and data sections mostly throwbacks to the 16bit segmented memory days?

 

I recall that when I learned to program in a compilable version of QuickBASIC (v4.5 for those who remember) they actually had instructions for setting the code and data segments that were active. One of the major optimizations I discovered in one of my programs once, was that the structure of my map rendering was causing me to jump all over memory and even across data segments -- which was obviously horrible. When I fixed it, I went from 5fps to 50fps, in a tight rendering loop where blitting should have been the bottleneck.

 

no, you mixing old 640K ds: cs: stuff here... 

i am moving about .data and .code sections in program image,

which are later mapped into a ram memory where ram pages are also mapped as "executable" "data" "readolny data" etc - and i wonder on the details of this here



#9 SeanMiddleditch   Members   -  Reputation: 4077

Like
1Likes
Like

Posted 27 February 2014 - 07:46 PM

and when counting the execution times it was much slower than 
executing it in normal code section - i dont know what was a reason of that but it worked but had a speed penalty sadly


I'm willing to bet you'd see a lot of overhead in the function that calls into your code array; jumps and address dereferences and so on. There's also a difference between the data L1 cache and the code L1 cache, and pulling data in to be executed likely incurs additional cache misses and requests in order to run. This is also a possible reason for data in the code section being slower. One would have to look at the disassembly and run under a suitably capable profiler (vtune, cachegrind, etc.) to verify that assumption. This is part of what locality of reference in code is all about and why a profile-guided linker will lay out the compiled code differently (so functions that call each other end up close together in the machine code image). Naively written SSE code can also incur a lot of overhead due to various modes and flags; compiler intrinsics are almost always the right way to go to avoid these kinds of issues thanks to the compiler being able to set things up properly when you're not hiding your use of SSE from the compiler.

In any case, this works on Windows because you have to opt in to the NX bit on Windows for backwards-compatibility reasons. It's usually on by default in Linux and most other major OSes that support the feature. The flag is set on by default with newer versions of Visual Studio, I think, but imported project files or projects built with older VS versions probably don't have the bit set in the executable image to enable the NX behavior. This is similar to how a 32-bit application has to opt in to being LAA (large address aware) to access more than 2GB of memory even on a 64-bit kernel.

#10 Nypyren   Crossbones+   -  Reputation: 3737

Like
1Likes
Like

Posted 27 February 2014 - 08:00 PM

Jump tables for switch statements are technically data which are typically stored in the code section after the end of the function which contains the switch statement.
 
Microsoft compilers output files organized like so:
 
 
Headers
Code Section (the headers designate this section as Execute+Read)
    Function 0
        Instructions
        Optional: NOP or INT3 padding
        Optional: Case mapping table (and optional padding to the next pointer-sized boundary)
        Optional: Case block pointers (and optional padding to the next 16 byte boundary)
    Function 1
        Instructions
        ...ETC...
Initialized Data Section(s) (the headers designate these section(s) as Read+Write or just Read)
    Data!
Import Section (readonly after the loader finishes with it)
    Data!
Export Section (readonly after the loader finishes with it)
    Data!
Relocation Section (discarded after the loader finishes with it)
    Data!
Resource Section (readonly)
    Data!
(etc...)
Other compilers (or binary compressors such as UPX) are free to rearrange anything besides the main header that they want to.

Edited by Nypyren, 27 February 2014 - 08:05 PM.


#11 fir   Members   -  Reputation: -441

Like
0Likes
Like

Posted 28 February 2014 - 12:44 AM

Jump tables for switch statements are technically data which are typically stored in the code section after the end of the function which contains the switch statement.
 
Microsoft compilers output files organized like so:
 
 

Headers
Code Section (the headers designate this section as Execute+Read)
    Function 0
        Instructions
        Optional: NOP or INT3 padding
        Optional: Case mapping table (and optional padding to the next pointer-sized boundary)
        Optional: Case block pointers (and optional padding to the next 16 byte boundary)
    Function 1
        Instructions
        ...ETC...
Initialized Data Section(s) (the headers designate these section(s) as Read+Write or just Read)
    Data!
Import Section (readonly after the loader finishes with it)
    Data!
Export Section (readonly after the loader finishes with it)
    Data!
Relocation Section (discarded after the loader finishes with it)
    Data!
Resource Section (readonly)
    Data!
(etc...)
Other compilers (or binary compressors such as UPX) are free to rearrange anything besides the main header that they want to.

 

 

but why they do that? case values table and also pointers table i think is data do maybe it would be better if it would be in .data



#12 fir   Members   -  Reputation: -441

Like
0Likes
Like

Posted 28 February 2014 - 12:53 AM

 

and when counting the execution times it was much slower than 
executing it in normal code section - i dont know what was a reason of that but it worked but had a speed penalty sadly


I'm willing to bet you'd see a lot of overhead in the function that calls into your code array; jumps and address dereferences and so on. There's also a difference between the data L1 cache and the code L1 cache, and pulling data in to be executed likely incurs additional cache misses and requests in order to run. This is also a possible reason for data in the code section being slower. One would have to look at the disassembly and run under a suitably capable profiler (vtune, cachegrind, etc.) to verify that assumption. This is part of what locality of reference in code is all about and why a profile-guided linker will lay out the compiled code differently (so functions that call each other end up close together in the machine code image). Naively written SSE code can also incur a lot of overhead due to various modes and flags; compiler intrinsics are almost always the right way to go to avoid these kinds of issues thanks to the compiler being able to set things up properly when you're not hiding your use of SSE from the compiler.

In any case, this works on Windows because you have to opt in to the NX bit on Windows for backwards-compatibility reasons. It's usually on by default in Linux and most other major OSes that support the feature. The flag is set on by default with newer versions of Visual Studio, I think, but imported project files or projects built with older VS versions probably don't have the bit set in the executable image to enable the NX behavior. This is similar to how a 32-bit application has to opt in to being LAA (large address aware) to access more than 2GB of memory even on a 64-bit kernel.

 

 

 

i can give more info from my notes, i was tested a loops

of 1000x with rdtsc and testet such codes (down from my notes)

 

1)

    vector.x = 1.0;       //2 cycles

2)

     data_foo();  //calling a pointer to such machine code array

                // where only "ret" was contained 

                  //12 cycles

 

3)
    vector.x = 1.0; 
    data_foo();

                     //mixing one with the second

                    // few hundred cycles - with unknown reason



#13 Nypyren   Crossbones+   -  Reputation: 3737

Like
0Likes
Like

Posted 28 February 2014 - 01:18 AM

but why they do that? case values table and also pointers table i think is data do maybe it would be better if it would be in .data


The only thing I can think of is that the processor may be able to opportunistically cache jump tables if they're located after the function, but I haven't read the cache section of the Intel documentation to know whether this is done or not. I focus more on reverse engineering than optimization, so cache behavior isn't something I know very well.

#14 fir   Members   -  Reputation: -441

Like
0Likes
Like

Posted 28 February 2014 - 01:27 AM

 

but why they do that? case values table and also pointers table i think is data do maybe it would be better if it would be in .data


The only thing I can think of is that the processor may be able to opportunistically cache jump tables if they're located after the function, but I haven't read the cache section of the Intel documentation to know whether this is done or not. I focus more on reverse engineering than optimization, so cache behavior isn't something I know very well.

 

agner fog mentioned that this mixing compilers do (may) degrade performance (though he not gave a details)

[I am recently interested in disasembly so im searching for claryfy in such topics, will be studying coff a little soon]



#15 King Mir   Members   -  Reputation: 1914

Like
0Likes
Like

Posted 28 February 2014 - 04:56 AM

In principle, putting intermixing constant data in your code section could benefit from cache locality. Less so with a split L1 cache. But it can be difficult to determine when such an optimization is worthwhile.

#16 fir   Members   -  Reputation: -441

Like
0Likes
Like

Posted 28 February 2014 - 05:28 AM

In principle, putting intermixing constant data in your code section could benefit from cache locality. Less so with a split L1 cache. But it can be difficult to determine when such an optimization is worthwhile.

 

OK, but I think it maybe is not sufficient to explain a strange few hundred cycles slowdown i noticed in above test - so maybe some other slowdown mechanizm is involved (when calling 

a code contained in the data section) - maybe i will be able to test and measure more things with hand assembly test but i coul ddo it later



#17 King Mir   Members   -  Reputation: 1914

Like
0Likes
Like

Posted 28 February 2014 - 12:40 PM

Few hundred cycles? that's like a L3 cache miss. That's totally in keeping with the fact that jumping to an arbitrary code block is likely to be far in memory from the previous execution.

#18 Nypyren   Crossbones+   -  Reputation: 3737

Like
0Likes
Like

Posted 28 February 2014 - 01:25 PM

i can give more info from my notes, i was tested a loops
of 1000x with rdtsc and testet such codes (down from my notes)
 
1)
    vector.x = 1.0;       //2 cycles[/size]

2)
     data_foo();  //calling a pointer to such machine code array
                // where only "ret" was contained 
                  //12 cycles[/size]
 
3)
    vector.x = 1.0; [/size]
    data_foo();
                     //mixing one with the second
                    // few hundred cycles - with unknown reason



Without knowing what the entirety of your program looks like, we can only speculate what you might be seeing. If you post the full program's disassembly (including the address of each function) we might be able to narrow down the issue more easily.

Everything matters. If 'vector' is a stack-allocated variable or a heap variable, that matters. The address of each function. All of the rest of the code that you've omitted also matters.

#19 fir   Members   -  Reputation: -441

Like
0Likes
Like

Posted 28 February 2014 - 01:46 PM

 

i can give more info from my notes, i was tested a loops
of 1000x with rdtsc and testet such codes (down from my notes)
 
1)
    vector.x = 1.0;       //2 cycles[/size]

2)
     data_foo();  //calling a pointer to such machine code array
                // where only "ret" was contained 
                  //12 cycles[/size]
 
3)
    vector.x = 1.0; [/size]
    data_foo();
                     //mixing one with the second
                    // few hundred cycles - with unknown reason



Without knowing what the entirety of your program looks like, we can only speculate what you might be seeing. If you post the full program's disassembly (including the address of each function) we might be able to narrow down the issue more easily.

Everything matters. If 'vector' is a stack-allocated variable or a heap variable, that matters. The address of each function. All of the rest of the code that you've omitted also matters.

 

I know but i got only notes now, lost the source  - can do test later, but i remember it was like that -assigment took 2 cycles , ret from data 12 cycles and one with another few hundred cycles instead of 14 or so..



#20 richardurich   Members   -  Reputation: 1187

Like
0Likes
Like

Posted 28 February 2014 - 03:27 PM

For the vector/data_foo() example, I'm guessing data_foo() jumps to an address in .data. That causes the bad performance. If you try to jump to code near data, you often force waiting for that data to be flushed far enough in the memory heirarchy to reach a unified portion of memory. Depending on your system's design, that worst case could technically require writing data all the way out to main memory. Best case is probably store to load forwarding not caring about icache/dcache designation, but even then you're likely still forcing a sync and taking the perf hit that entails.

 

If you're really interested in this stuff, you can read about dynamic recompilation. It's largely avoided nowadays, so you're likely going to find information relating to Pentium 3 and Athlon if not even older than that. You might even find open source emulators/interpreters that still use dynamic recompilation since I know there was a lot of love for dynamic recompilation like 10-15 years ago.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS