Jump to content

HELP!!!!

- - - - -

  • Please log in to reply
13 replies to this topic

#1
untitled_1

untitled_1

    Learning Programmer

  • Members
  • PipPipPip
  • 89 posts
Hi, am having a bit of trouble with dynamic linking, i am using linux mint the 64bit version, I get errors when i try dynamically link.
here is a simple program that uses calls to c functions:

.section .data
    values: .int 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60
    output: .asciz "The value is %d\n"
    
.section .text
.globl _start
    _start:
        nop
        movl $0, %edi
        xor %rax, %rax
        loop:
            movl values(, %edi, 4 ), %eax
            pushq %rax
            pushq $output
            call printf
            
            inc %edi
        cmpl $11, %edi
        jne loop
        
        movl $1, %eax
        movl $0, %ebx
        int $0x80

have tried linking with two different libraries

the first one i typed: ld -dynamic-link /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 -lc -o frog frog.o
with this i got the following error : "Segmentation fault.
I know the main cause is the the call to printf

the second thing i tried was : ld -dynamic-link /lib/ld-linux.so.2 -lc -o frog frog.o
with this the error i got was: "bash: ./frog: Accessing a corrupted shared library"

is there like i specific library i must use or download

Edited by dargueta, 03 December 2011 - 02:23 PM.


#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
You're not cleaning up the stack after calling printf. When you try to return, your data is still on the stack and the processor will interpret that as a return address.

As for the second error, fix the first and we'll go from there.
sudo rm -rf /

#3
untitled_1

untitled_1

    Learning Programmer

  • Members
  • PipPipPip
  • 89 posts
Thanks, the clean up of the stack seemed to have helped, but now when i run the program nothing gets printed in the terminal.
I also have another question: When i try to type the following pushl eax, i get an error "invalid suffix for push" i dont understand why because this instrustion is used in source code

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
pushl %eax

Try replacing the call to printf with a function of your own that prints out something hard-coded. If that doesn't show up, then printf is never getting called.
sudo rm -rf /

#5
untitled_1

untitled_1

    Learning Programmer

  • Members
  • PipPipPip
  • 89 posts
oh, ye that was just a typo, however I have found out that you cant push 32 bits onto the stack any more when programming for 64bits, so basically it seems like pushl has been dropped, I find that strange but there must be some reason or another.

#6
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,252 posts
  • Location:C:\Countries\US
I wonder if you can do it manually.

I don't know much of the 64-bit language, but I think it might be similar to this:
mov rcx, rax 

mov rax, qword [rsp] 

shl rax, 32 

mov eax, ecx 

sub rsp, 4 

mov qword [rsp], rax 

Once again, I don't know 64-bit, so I'm not completely sure if everything in the above code is efficient, or even correct.

#7
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Don't try to push 8-, 16-, or 32-bit data onto the stack. It needs to remain aligned on a 64-bit boundary for hardware reasons.
sudo rm -rf /

#8
untitled_1

untitled_1

    Learning Programmer

  • Members
  • PipPipPip
  • 89 posts
So the command line arguments are passed using 64 bits as well right?

#9
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Those are all passed in as strings, actually. The program converts the strings into numbers or whatever else it needs them to be.
sudo rm -rf /

#10
untitled_1

untitled_1

    Learning Programmer

  • Members
  • PipPipPip
  • 89 posts
I see, so the pointers to the command line arguments will be 64 bit addresses though? The amd64 manual I am using doesn't say much on this.

#11
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
That's because it's operating-system dependent; there's no real standard convention as far as that goes, but the C standard guarantees that command-line arguments are passed in as an array of string pointers.
sudo rm -rf /

#12
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,252 posts
  • Location:C:\Countries\US
And pointers, in the 64-bit world, are 64 bits in size, correct?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users