3

I have an application consisting of mixed C and x86 assembly code. For complex reasons, the assembly code wants to know if a (purported program counter) value is an address in the area in which the object code for the C code exists. This application has worked very well in the past, including this oddball check.

All I actually need are "bookends" on the C code area whose addresses I trust. This of course assumes that the compiled C code is deposited in the load image relatively densely.

I currently get my bookends by defining dummy functions LowestAddress(){} and HighestAddress(){} and placing them respectively first and last in the file containing the code. The assembly code simply compares a value of interest to the the addresses of these functions to determine if that value is in the C code range. Oops, this makes a second assumption, that functions are ordered in memory in source file text order, which used to apparantly be true with much older versions of Visual Studio.

Alas, no longer, certainly not with VS2010 compiling with link-time code generation. (I don't know what other [non]optimizations settings do.) On inspection with VS2010, my LowestAddress function is clearly in the middle of the object code for a bunch of other C functions.

How can I (re)implement these bookends? Some kind of segment control? The options available for build in VS 2010 don't seem to offer anything useful. (I understand that a really smart compiler might actually re-arrange the code to minimize cache line conflicts based on a call graph. Is MS actually that smart?). I don't really insist on ordering all the functions in this region by text order; I just need the upper and lower bounds of the region.

[I'm going to try turning off link-time code generation, which is the only place that a [global] call graph could be constructed, and see what effect that has.]

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • 2
    Welcome to Stack Overflow! Please be aware that tags are not keywords. That is, stuf-*wait*. 33k rep, and you just created new tags `visual` and `studio`?! C'mon, Ira! ;) – Charles Aug 24 '12 at 02:57
  • I had some weird browser issue as I entered the question and lost access to it until just now. I would have probably gotten it right but it is always good to have competent help. – Ira Baxter Aug 24 '12 at 03:19
  • The 2 options I can think of are the linker option /ORDER with which you can specify the order of functions in the executable and maybe specifying sections with the help of #pragma section. But I didn't try these. – Christopher Aug 24 '12 at 14:44
  • You mean: http://msdn.microsoft.com/en-us/library/00kh39zz.aspx? (Digging for the meaning of "COMDAT", my linker command line includes "/Gy-" which I think turns it off. ... COMDAT seems to package functions seperately so that one can use the /ORDER command on them. Maybe that's the right answer. It'll take me some time to go try this. – Ira Baxter Aug 24 '12 at 14:54
  • @Christopher: the /ORDER directive turned out to be the trick, using my dummy functions as first and list elements in the list. I had to use the "/Gy-" function on the file that contained the functions I wanted to order, and had to build a file containing a list of link-edit function names, but that all worked. (I never did figure out what COMDAT meant exactly but I don't care :) Finally, a MS feature that does what it says without tears! Post a real answer so I can give you credit. – Ira Baxter Oct 19 '12 at 23:24

1 Answers1

2

You can specify the order of how functions are placed in a image by using the /ORDER compiler parameter.

Christopher
  • 8,912
  • 3
  • 33
  • 38