LinuxPPC-Dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Anand Android <android.anand@gmail.com>
To: linuxppc-dev@lists.ozlabs.org
Subject: Reading command line args from assmebly
Date: Thu, 18 Feb 2010 16:45:31 +0530	[thread overview]
Message-ID: <7d50427c1002180315k38a35182s9532eedd107e5fed@mail.gmail.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 1544 bytes --]

Hi All,
I have written one sample assmebly and c code which reads command line args
and displays .

find the source files as attachments.

expected output is ,
        $ ./a.out
           Inside __linker_init argc = 1
           Inside __linker_init argv[0] = ./a.out
           Segmentation fault [Seg fault Never mind]

if i statically link startx86.S  & linker.c  on x86 AND statrt_ppc.S &
linker.c on PS3,i'll get SEGV fault on both system. But on PS3, inside
'_start' function if i verify the contents of 'r1[stack pointer]' and '(Char
*) *(r1 + 4)'  in GDB i get '1' and '~/a.out' respectively .But once 'r1'
moved to r3[holds the 1st arg of the function] & passed as argument to
__linker_init ,inside the function '*elfdata' points '1' but (char *)
*(elfdata + 1) points to some junk string. some thing like this
"|c\033x|i\003�N\200\004 \224!��|\b\002�\220\001".
find the GDB log file of PS3.

if i link them without statically,i'll get the correct output on x86 .i.e

        $ ./a.out
           Inside __linker_init argc = 1
           Inside __linker_init argv[0] = ./a.out
           Segmentation fault [Seg fault Never mind]

but  on ps3 i get this,
        $ ./a.out
           Inside __linker_init argc = 0
           Inside __linker_init argv[0] = |c|i�N� �!����
           Segmentation fault
Why am i getting strange characters instead of './a.out' and y 'argc' having
'zero'  ?

Why is this two different outputs for static linking and dynamic linking ?

-Anand

[-- Attachment #1.2: Type: text/html, Size: 1915 bytes --]

[-- Attachment #2: gdb.txt --]
[-- Type: text/plain, Size: 2129 bytes --]

This GDB was configured as "powerpc-linux-gnu"...                            
(gdb) b _start
Breakpoint 1 at 0x100000e4: file start.S, line 35.
(gdb) r                                           
Starting program: /home/dayananda/junks/power/a.out

Breakpoint 1, _start () at start.S:37
37              bl      __linker_init
Current language:  auto; currently asm
(gdb) s                               
__linker_init (elfdata=0x0) at linker.c:5
5        {                               
Current language:  auto; currently c     
(gdb) i r
r0             0x0      0
r1             0xff9a1820       4288288800
r2             0x0      0
r3             0xff9a1820       4288288800
r4             0x0      0
r5             0x0      0
r6             0x0      0
r7             0x0      0
r8             0x0      0
r9             0x0      0
r10            0x0      0
r11            0x0      0
r12            0x0      0
r13            0x0      0
r14            0x0      0
r15            0x0      0
r16            0x0      0
r17            0x0      0
r18            0x0      0
r19            0x0      0
r20            0x0      0
r21            0x0      0
r22            0x0      0
r23            0x0      0
r24            0x0      0
r25            0x0      0
r26            0x0      0
r27            0x0      0
r28            0x0      0
r29            0x0      0
r30            0x0      0
r31            0x0      0
pc             0x100000f4       268435700
msr            0x4010c032       1074839602
cr             0x0      0
lr             0x100000e8       268435688
ctr            0x0      0
xer            0x0      0
(gdb) p *(0xff9a1820)
$1 = 1
(gdb) p *(0xff9a1820+4)
$2 = -6678169
(gdb) p (char *)*(0xff9a1820+4)
$3 = 0xff9a1967 "/home/dayananda/junks/power/a.out"
(gdb) s
7               int argc = (int) *elfdata;
(gdb) s
8               char **argv = (char**) (elfdata + 1);
(gdb) p elfdata
$4 = (unsigned int **) 0xff9a1820
(gdb) p *elfdata
$5 = (unsigned int *) 0x1
(gdb) p *(elfdata + 1)
$6 = (unsigned int *) 0x100000e8
(gdb) p (char *)*(elfdata + 1)
$7 = 0x100000e8 "|c\033x|i\003�N\200\004 \224!��|\b\002�\220\001"



[-- Attachment #3: linker.c --]
[-- Type: application/octet-stream, Size: 278 bytes --]


#include<stdio.h>


unsigned __linker_init(unsigned **elfdata)
 {
 
	int argc = (int) *elfdata;
	char **argv = (char**) (elfdata + 1);

	printf("Inside __linker_init argc = %d\n", argc );	
	printf("Inside __linker_init argv[0] = %s\n", argv[0]);

	return 0;
}


[-- Attachment #4: start_ppc.S --]
[-- Type: application/octet-stream, Size: 1745 bytes --]

/*
 * Copyright (C) 2008 The Android Open Source Project
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

	.text
	.align 4
	.type _start,@function
	.globl _start

_start:
	mr	3,1
	bl	__linker_init

	/* linker init returns the _entry address in the main image */
/* 	return value will be in r3,*/
	mr 3,3
	mtctr	3
	bctr			/*branch the address stored in the r3 i.e count register*/ 

	.section .ctors, "wa"
	.globl __CTOR_LIST__
__CTOR_LIST__:
	.long -1

[-- Attachment #5: start_x86.S --]
[-- Type: application/octet-stream, Size: 556 bytes --]


  .text
  .align 4
  .type _start, @function
  .globl _start
  
  _start:
          /* save the elfdata ptr to %eax, AND push it onto the stack */
          mov    %esp, %eax
          pushl  %esp
  
          pushl  %eax
          call   __linker_init
  
          /* linker init returns (%eax) the _entry address in the main image */
          /* entry point expects sp to point to elfdata */
          popl   %esp
          jmp    *%eax
  
  .section .ctors, "wa"
  .globl __CTOR_LIST__
  
  __CTOR_LIST__:
          .long -1


                 reply	other threads:[~2010-02-18 11:15 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7d50427c1002180315k38a35182s9532eedd107e5fed@mail.gmail.com \
    --to=android.anand@gmail.com \
    --cc=linuxppc-dev@lists.ozlabs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox