Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6988369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:04:22+00:00 2026-05-27T19:04:22+00:00

I am having trouble linking 2 object files one of which was generated from

  • 0

I am having trouble linking 2 object files one of which was generated from an Assembly Language Source File and another that was generated from a C Source file.

C source code:

//main2.c
extern int strlength(char *);
int main(){
    char * test = "hello";
    int num = strlength(test);
    return num;
}

Assembly source code:

#strlength.s
.include "Linux32.s"

.section .text
.globl strlength
.type strlength, @function
strlength:
 pushl %ebp
 movl %esp, %ebp
 movl $0, %ecx
 movl 8(%ebp), %edx
read_next_byte:
 movb (%edx), %al
 cmpb $END_OF_FILE, %al
 jle end
 incl %edx
 incl %ecx
 jmp read_next_byte
end:
 movl %ecx, %eax
 popl %ebp
 ret

When I compile and run using ‘gcc’ like this:

gcc main2.c strlength.s -m32 -o test
./test
echo $?

I get 5 which is correct. However when I compile/assemble separately and then link with ‘ld’
like this:

as strlength.s --32 -o strlength.o
cc main2.c -m32 -o main2.o
ld -melf_i386 -e main main2.o strlength.o -o test
./test

I get a segmentation fault. What is causing this? Am I not following the C calling convention 100% correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:04:23+00:00Added an answer on May 27, 2026 at 7:04 pm

    ld -melf_i386 -e main main2.o strlength.o -o test

    Don’t do that. Do this instead:

    gcc -m32 main2.o strlength.o -o test
    

    (You should probably not call your test exectuable test, as it may conflict with the /bin/test, standard on most UNIX systems.)

    Explanation: UNIX binaries do not generally start executing at main. They start executing in a function called _start, which comes from crt1.o or similar (“C Runtime startup”). That file is part of libc, and it arranges for various initializations, required for the proper start up of your application.

    Your program actually doesn’t require anything from libc, which is why you were able to link it with ld.

    However, consider what happens after your main returns. Normally, the code in crt1.o will execute (equivalent of) exit(main(argc, argv));. Since you linked without crt1.o, there is nobody to do that final exit for you, so the code returns to … undefined location and promptly crashes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble with the linking of my files. Basically, my program consists of:
Im having trouble reading from a CSV file final String DELIMITER = ,; Scanner
I am having trouble linking to a style sheet from a jsp page. I
I'm having trouble statically linking an app which uses the boost 1.35 libraries. I'm
I'm having trouble building a simple c++ program that tests out regex's from the
Having trouble linking the Stomp.framework into an iPhone SDK application. http://code.google.com/p/stompframework/ I follow the
Having trouble with proper regex for RewriteCond RewriteCond %{REQUEST_URI} !^/foo/ Works as expected, that
having trouble getting my directx going I get the following error 1>Linking... 1>main.obj :
I'm having trouble getting my dll to work when using explicit linking. Using implicit
I having trouble with app that uses my static libs. Here is my setup:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.