Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | jt@toerring.de (Jens Thoms Toerring) |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Relocatable functions in C? |
| Date | 2015-08-16 01:26 +0000 |
| Organization | Freie Universitaet Berlin |
| Message-ID | <d3a76gF5jsU1@mid.uni-berlin.de> (permalink) |
| References | <2764e28b-d6e9-44d5-8ff4-b612a3d7e9a4@googlegroups.com> <e258aa22-2f86-49b7-98a6-1be80f4fcd2c@googlegroups.com> |
Rick C. Hodgin <rick.c.hodgin@gmail.com> wrote:
> I have discovered I can use this type of file (very simple for the
> sake of this test):
> int my_function(void)
> {
> return(-5);
> }
> And this command line:
> gcc -masm=intel -S fred.c
> ...generated a file which I edited down manually for making a simpler
> test. The final fred.s file looked like this:
> -----[ Begin ]-----
> .file "fred.c"
> .intel_syntax noprefix
> .text
> .globl _my_function
> .def _my_function; .scl 2; .type 32; .endef
> _my_function:
> LFB0:
> mov eax, -5
> ret
> LFE0:
> .ident "GCC: (GNU) 4.8.1"
> -----[ End ]-----
> Next, I used this command to assemble and link the edited fred.s
> file:
> gcc -nodefaultlibs -nostdlib -o fred.dll fred.s
> It generates fred.dll, a 4 KB file. Running fred.dll through the
> objdump utility:
> objdump -x fred.dll > fred.dmp
> ...I get a 7 KB file, which contains this (pared down for display here):
> Sections:
> Idx Name Size VMA LMA File off Algn
> 0 .text 00000018 00401000 00401000 00000200 8 bytes
> 1 .rdata 00000014 00402000 00402000 00000400 8 bytes
> 2 .idata 00000014 00403000 00403000 00000600 8 bytes
> Size shows the section is 0x18 bytes long, and File off shows it
> begins at offset 0x200 in the .dll file itself. I wasn't sure if
> this was right or not, so I went to examine it manually.
> Looking at the contents of the fred.dll file in a hex editor, I find
> this content at offset 0x200:
> 00000200 .. B8 FB FF FF FF C3 90 90 .. FF FF FF FF 00 00 00 00
> 00000210 .. FF FF FF FF 00 00 00 00 .. 00 00 00 00 00 00 00 00
> Running those bytes through a disassembler, I see that it disassembles
> to these assembly opcodes:
> B8 FB FF FF FF mov eax,0FFFFFFFBh (-5)
> C3 ret
> ; Padding bytes to round to nearest 8-byte alignment:
> 90 nop
> 90 nop
> ; The remaining bytes are just padding
> -----
> This gives me a way to compile code, run it through objmap, get the
> offset and size, and load it in directly from the DLL without having
> to use any loaders. I do not have to use any global memory, and can
> pass everything in by parameters. I discovered by using -fPIC that
> the compiler told me it's ignored, that all code is generated as
> position independent by default ... which is good news.
On a x86-64 doing
gcc -shared -fpic -o fred.so fred.c
gives you the same results (also a file of a bit more than 7 kB).
From
objdump -x fred.so I find
I get a line line with
0000000000000668 g F .text 0000000000000006 my_function
And, voila, at 0x668 in that file I have
00000660: e94b ffff ff90 9090 b8fb ffff ffc3 9090 .K..............
where the length of 6 bytes for that function actually looks
correct (it just includes the '0xc3', i.e. "return"). And, if
you start having something in the there that does jumps it
should be position-independent, which your approach rather
likely doesn't guarantee.
> Now, I have to tackle the issue of having executable code running
> in allocated data. But, apart from that everything is working. If
> worse comes to worst, I'll just write a kernel-level driver which
> handles that part for me.
The kernel probably won't do much for you in that respect with
a driver. But, probably, if your program runs with root permis-
sions, there are ways to fiddle with the page flags. Much fun
figuring out that and then getting people to install your pro-
gram so that it always runs with full root permission. But, then,
for installing a kernel driver to get your program to run at all,
would require the same amount of trust...
Simply doing a dlopen() and then dlsym() on 'my_function'
would avoid all that hassle (as well as taking care of fixing
all addresses of functions that may be called from within
'my_function'). But if you want to do it the hard way no-one
is keeping you back;-) Just better start reading lots and
lots of documentation. And, most important, keep in mind that
all your "exoeriments" are done on a certain machine with a
certain OS. Make up your mind what's your target! No shame in
saying "This program will only run under archtecture X when
using operating system Y", but make that a decision, not
something that's merely the result of not understanding what
you were doing!
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Relocatable functions in C? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-08-14 09:57 -0700
Re: Relocatable functions in C? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-08-14 10:13 -0700
Re: Relocatable functions in C? Ken Brody <kenbrody@spamcop.net> - 2015-08-14 13:33 -0400
Re: Relocatable functions in C? jacobnavia <jacob@jacob.remcomp.fr> - 2015-08-14 19:51 +0200
Re: Relocatable functions in C? Robert Wessel <robertwessel2@yahoo.com> - 2015-08-14 16:12 -0500
Re: Relocatable functions in C? David Brown <david.brown@hesbynett.no> - 2015-08-15 16:17 +0200
Re: Relocatable functions in C? Bartc <bc@freeuk.com> - 2015-08-14 18:29 +0100
Re: Relocatable functions in C? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-08-14 11:37 -0700
Re: Relocatable functions in C? Bartc <bc@freeuk.com> - 2015-08-14 19:43 +0100
Re: Relocatable functions in C? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-08-14 11:46 -0700
Re: Relocatable functions in C? Bartc <bc@freeuk.com> - 2015-08-14 19:57 +0100
Re: Relocatable functions in C? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-08-14 11:59 -0700
Re: Relocatable functions in C? jt@toerring.de (Jens Thoms Toerring) - 2015-08-14 20:27 +0000
Re: Relocatable functions in C? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-08-14 12:01 -0700
Re: Relocatable functions in C? Waldek Hebisch <hebisch@antispam.uni.wroc.pl> - 2015-08-25 22:36 +0000
Re: Relocatable functions in C? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-08-26 05:55 -0700
Re: Relocatable functions in C? fir <profesor.fir@gmail.com> - 2015-08-14 12:57 -0700
Re: Relocatable functions in C? fir <profesor.fir@gmail.com> - 2015-08-14 13:26 -0700
Re: Relocatable functions in C? fir <profesor.fir@gmail.com> - 2015-08-14 13:39 -0700
Re: Relocatable functions in C? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-08-14 13:46 -0700
Re: Relocatable functions in C? fir <profesor.fir@gmail.com> - 2015-08-14 14:00 -0700
Re: Relocatable functions in C? fir <profesor.fir@gmail.com> - 2015-08-14 15:07 -0700
Re: Relocatable functions in C? fir <profesor.fir@gmail.com> - 2015-08-14 16:02 -0700
Re: Relocatable functions in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-08-14 23:11 -0700
Re: Relocatable functions in C? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-15 10:31 +0100
Re: Relocatable functions in C? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-08-15 11:56 -0700
Re: Relocatable functions in C? jacobnavia <jacob@jacob.remcomp.fr> - 2015-08-15 23:48 +0200
Re: Relocatable functions in C? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-08-15 15:05 -0700
Re: Relocatable functions in C? jacobnavia <jacob@jacob.remcomp.fr> - 2015-08-16 00:12 +0200
Re: Relocatable functions in C? David Brown <david.brown@hesbynett.no> - 2015-08-16 16:09 +0200
Re: Relocatable functions in C? Ian Collins <ian-news@hotmail.com> - 2015-08-16 10:47 +1200
Re: Relocatable functions in C? jacobnavia <jacob@jacob.remcomp.fr> - 2015-08-16 01:09 +0200
Re: Relocatable functions in C? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-08-15 15:07 -0700
Re: Relocatable functions in C? jt@toerring.de (Jens Thoms Toerring) - 2015-08-15 22:09 +0000
Re: Relocatable functions in C? jacobnavia <jacob@jacob.remcomp.fr> - 2015-08-16 00:21 +0200
Re: Relocatable functions in C? jt@toerring.de (Jens Thoms Toerring) - 2015-08-15 23:44 +0000
Re: Relocatable functions in C? jacobnavia <jacob@jacob.remcomp.fr> - 2015-08-16 02:25 +0200
Re: Relocatable functions in C? jacobnavia <jacob@jacob.remcomp.fr> - 2015-08-16 02:34 +0200
Re: Relocatable functions in C? jt@toerring.de (Jens Thoms Toerring) - 2015-08-16 01:11 +0000
Re: Relocatable functions in C? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-08-15 15:26 -0700
Re: Relocatable functions in C? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-08-15 15:36 -0700
Re: Relocatable functions in C? jt@toerring.de (Jens Thoms Toerring) - 2015-08-15 23:29 +0000
Re: Relocatable functions in C? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-08-15 16:34 -0700
Re: Relocatable functions in C? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-08-15 17:03 -0700
Re: Relocatable functions in C? jacobnavia <jacob@jacob.remcomp.fr> - 2015-08-16 02:32 +0200
Re: Relocatable functions in C? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-08-15 18:25 -0700
Re: Relocatable functions in C? jt@toerring.de (Jens Thoms Toerring) - 2015-08-16 01:26 +0000
Re: Relocatable functions in C? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-08-15 18:38 -0700
Re: Relocatable functions in C? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-08-15 18:49 -0700
Re: Relocatable functions in C? Ian Collins <ian-news@hotmail.com> - 2015-08-16 14:04 +1200
Re: Relocatable functions in C? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-08-15 20:19 -0700
csiph-web