Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > alt.os.development > #9564
| Newsgroups | alt.os.development |
|---|---|
| Date | 2016-05-25 15:02 -0700 |
| References | <06b38764-b3ee-4eed-9b82-f62c1d68b0ec@googlegroups.com> |
| Message-ID | <ba3e35ec-d8f7-4d00-940b-e04c1ba1d581@googlegroups.com> (permalink) |
| Subject | Re: Questions about the Implementation of the ELF format |
| From | "Isaac D. Cohen" <8switchsoftware@gmail.com> |
> Paging exists to fool big fat programs into believing they have
> lots of fast and expensive RAM to use when in fact most of it
> is backed by your cheap and slow disk storage. Virtual memory
> it is.
It also exists to solve the relocation problem. :)
> Position-independent code is a good thing, but it's not always
> cheap (the CPU simply may have no decent PC-relative addressing)
> and it itself may need a tiny bit of relocation.
> Let's say you have this tiny bit of C code:
> #include <stdio.h>
> int a = 123;
> int* pa = &a;
> int main(void)
> {
> printf("%d\n", *pa);
> return 0;
> }
> On the x86, direct calls are PC-relative, so calling printf()
> may be deemed covered in terms of position independence.
> But data accesses to pa and a in non-64-bit modes are not
> PC-relative. Hence relocations and/or complex indirection
> schemes.
> OTOH, in the 64-bit mode you can access both pa and a relative
> to PC. However, how does pa get its initial value, the address
> of a? There must be some code somewhere that takes the address
> of a and stores it in memory at the address of pa. That is a form
> of relocation. Bummer!
But what does ELF have to do with this whole thing? I understand that certain
things are relative and certain things have to be relocated. But what does ELF
have to do with it? Why can't we just compile position independent code when we
need it and when not, use pure binary files? Obviously we would need paging in
order to solve the relocation problem with these pure binary files, but wouldn't
the same thing be necessary for ELFs?
Mike Gonta wrote:
> No need to if you're going to generate a flat binary file.
> Simple link the object files and specify:
> OUTPUT_FORMAT("binary")
> in the linker file.
>> because otherwise, I won’t know how to jump into the
>> executable from an assembly file in my OS.
> Simply link with the asm object file and include a jmp or
> call to the function name used in the "C" file.
> Another simple technique to use when running a loaded flat
> binary from the OS is to call the beginning address of the
> loaded file. In this case since you can never be sure of
> where the actually address of the "main" function will be in
> the actual binary simply put an inline asm statement at the
> end of a "#include"d file:
> __asm__ ("jmp main");
> and the linker will take care of the rest.
> Also, for example, if you load the the file to (for example)
> 0x400000, you can call it from "C" in the OS loader with:
> ((int (*)(void ))0x400000)();
> (with the appropriate return type and parameters).
So let's say I write an assembly file and a C file and then I link them together.
How would I call this executable file from an assembly file in my OS? If I load
it to 0x400000, can I just write "jmp 0x400000"? I think this would only work if
the code of the assembly file (that was linked in) is placed (by the linker)
right at the beginning of the resulting executable. Would that be the case?
Thank you very much,
Isaac D. Cohen
Back to alt.os.development | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Questions about the Implementation of the ELF format "Isaac D. Cohen" <8switchsoftware@gmail.com> - 2016-05-23 22:07 -0700
Re: Questions about the Implementation of the ELF format Rod Pemberton <NoHaveNotOne@bcczxcfre.cmm> - 2016-05-24 03:48 -0400
Questions about the Implementation of the ELF format Peter Cheung <mcheung63@gmail.com> - 2016-05-24 01:10 -0700
Re: Questions about the Implementation of the ELF format "Alexei A. Frounze" <alexfrunews@gmail.com> - 2016-05-24 01:18 -0700
Re: Questions about the Implementation of the ELF format "Mike Gonta" <mikegonta@gmail.com> - 2016-05-24 10:34 -0400
Re: Questions about the Implementation of the ELF format Herbert Kleebauer <klee@unibwm.de> - 2016-05-24 17:24 +0200
Re: Questions about the Implementation of the ELF format "Isaac D. Cohen" <8switchsoftware@gmail.com> - 2016-05-24 14:18 -0700
Re: Questions about the Implementation of the ELF format "Alexei A. Frounze" <alexfrunews@gmail.com> - 2016-05-25 02:21 -0700
Re: Questions about the Implementation of the ELF format "Isaac D. Cohen" <8switchsoftware@gmail.com> - 2016-05-25 15:02 -0700
Re: Questions about the Implementation of the ELF format "Mike Gonta" <mikegonta@gmail.com> - 2016-05-25 19:03 -0400
Re: Questions about the Implementation of the ELF format "Isaac D. Cohen" <8switchsoftware@gmail.com> - 2016-05-26 14:51 -0700
Re: Questions about the Implementation of the ELF format Rod Pemberton <NoHaveNotOne@bcczxcfre.cmm> - 2016-05-27 02:55 -0400
Re: Questions about the Implementation of the ELF format "Isaac D. Cohen" <8switchsoftware@gmail.com> - 2016-05-27 13:43 -0700
Re: Questions about the Implementation of the ELF format "Mike Gonta" <mikegonta@gmail.com> - 2016-05-27 17:40 -0400
Re: Questions about the Implementation of the ELF format "Isaac D. Cohen" <8switchsoftware@gmail.com> - 2016-05-28 20:15 -0700
Re: Questions about the Implementation of the ELF format Rod Pemberton <NoHaveNotOne@bcczxcfre.cmm> - 2016-05-29 02:35 -0400
Re: Questions about the Implementation of the ELF format "Isaac D. Cohen" <8switchsoftware@gmail.com> - 2016-06-15 13:51 -0700
Re: Questions about the Implementation of the ELF format Rod Pemberton <NoHaveNotOne@bcczxcfre.cmm> - 2016-05-27 03:09 -0400
Re: Questions about the Implementation of the ELF format "Alexei A. Frounze" <alexfrunews@gmail.com> - 2016-05-27 00:10 -0700
Re: Questions about the Implementation of the ELF format Rod Pemberton <NoHaveNotOne@bcczxcfre.cmm> - 2016-06-02 16:25 -0400
Re: Questions about the Implementation of the ELF format Rod Pemberton <NoHaveNotOne@bcczxcfre.cmm> - 2016-05-27 02:38 -0400
Re: Questions about the Implementation of the ELF format "Mike Gonta" <mikegonta@gmail.com> - 2016-05-27 04:11 -0400
Re: Questions about the Implementation of the ELF format "Alexei A. Frounze" <alexfrunews@gmail.com> - 2016-05-25 22:18 -0700
csiph-web