Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
In assembly is used to do something like this, to find the last address of my code for later storage purposes, or creating a dynamic memory management:
Start:
mov eax,End ; mov eax,offset End ; alternatively, ya know.
...
...
End:
Now EAX is pointing to the end of the code. But I've just started coding in C and I have no idea of finding this address after linking. I found that in C the first global variable of type void* points to what i need:
void* lastAddress;
int main(){
...
...
}
// <-lastAddress points to this address after linking.
Of course if I add some more variables after that "lastAddress" it won't point to the end of the program. Is there any way of keeping that "lastAddress" the last variable after linking? I've been thinking about dividing bss section in my linker script but I don't seem to come to an idea that worked.. P.S. I'm linking several object files btw.
Archil wrote:In assembly is used to do something like this, to find the last address of my code for later storage purposes, or creating a dynamic memory management: [...] But I've just started coding in C and I have no idea of finding this address after linking. I found that in C the first global variable of type void* points to what i need:
void* lastAddress;
int main(){
...
...
}
// <-lastAddress points to this address after linking.
You can't really rely on this. If you change your linker script and put the data sections somewhere else (e.g. in front of .text) the above assumption will fail.
Archil wrote:Of course if I add some more variables after that "lastAddress" it won't point to the end of the program. Is there any way of keeping that "lastAddress" the last variable after linking? I've been thinking about dividing bss section in my linker script but I don't seem to come to an idea that worked.. P.S. I'm linking several object files btw.
If your linker script defines "end" You could do something like:
Archil wrote:Hope this post will be helpful to others as well.
Not really. This is covered in bare bones and JamesM tutorials if I'm not mistaken.
Very likely, you are mistaken.
The wiki's bare bones article(s) don't have that. The linker scripts there don't even define "end" or something alike. It's just not needed there.
I had a (very!) quick look at JamesM's tutorials. He has a linker script, where the symbols "end", "_end" and "__end" are defined. But he doesn't use them in the "C world", as far as I have seen. He only uses "end" in his multiboot info in the "assembly world".
Archil wrote:Hope this post will be helpful to others as well.
Not really. This is covered in bare bones and JamesM tutorials if I'm not mistaken.
Very likely, you are mistaken.
The wiki's bare bones article(s) don't have that. The linker scripts there don't even define "end" or something alike. It's just not needed there.
I had a (very!) quick look at JamesM's tutorials. He has a linker script, where the symbols "end", "_end" and "__end" are defined. But he doesn't use them in the "C world", as far as I have seen. He only uses "end" in his multiboot info in the "assembly world".
Regards,
Thilo
You are fail, it seems.
I allude to &end in the tutorial description but never describe it explicitly - I use a variable called "placement_address" which is never initialised in the tutorial description - It's left as an exercise to the user. However, in the sample code (downloadable at the end of each tutorial, in this case tutorial 6) uses &end to initialise placement_address.
Hyperdrive wrote:[...] I had a (very!) quick look at JamesM's tutorials. He has a linker script, where the symbols "end", "_end" and "__end" are defined. But he doesn't use them in the "C world", as far as I have seen. He only uses "end" in his multiboot info in the "assembly world".
You are fail, it seems.
I allude to &end in the tutorial description but never describe it explicitly - I use a variable called "placement_address" which is never initialised in the tutorial description - It's left as an exercise to the user. However, in the sample code (downloadable at the end of each tutorial, in this case tutorial 6) uses &end to initialise placement_address.
Okay, you are right. Sorry. I definitely overlooked this. As I said, I had just more like a glance and never worked myself through your text.
Btw - From what I saw, your tutorials are really great. You take the reader through all important steps and your explanations are very well written. That isn't the case for many (most?) tutorials around the net.