Solved

manually assembling and linking gcc ouput source files

Posted on 2010-09-10
14
666 Views
Last Modified: 2012-05-10
I have gcc creating asm source files like this.

m68hc11-gcc -g -Wall -Werror -Winline -O -m68hcs12 -mshort -ffunction-sections -fomit-frame-pointer -msoft-reg-count=8 -mauto-incdec -fsigned-char -x cpp-output -S -o assembly/main.s preprocessedc/main.pp.c

from that output I can manually assemble it like this

m68hc11-as main.s -o main.o -m68hcs12

 or like this

m68hc11-gcc -g -Wall -Werror -Winline -O -m68hcs12 -mshort -ffunction-sections -fomit-frame-pointer -msoft-reg-count=8 -mauto-incdec -fsigned-char -c -o objects/freeEMS.o assembly/freeEMS.s
and it wont give me any errors

but when I try to link, I get this.
m68hc12-ld -defsym vectors_addr=0xF710 -m m68hc12elfb -T hc9s12xdp512elfb.x -o output/freeems-0.1.1-SNAPSHOT-Simple.elf objects/Simple.o objects/freeEMS.o objects/staticInit.o objects/main.o objects/tableLookup.o objects/init.o objects/utils.o objects/globalConstants.o objects/coreVarsGenerator.o objects/derivedVarsGenerator.o objects/fuelAndIgnitionCalcs.o objects/flashWrite.o objects/commsCore.o objects/blockDetailsLookup.o objects/interrupts.o objects/injectionISRs.o objects/ignitionISRs.o objects/commsISRs.o objects/realtimeISRs.o objects/miscISRs.o objects/FixedConfig1.o objects/FixedConfig2.o objects/IATTransferTable.o objects/CHTTransferTable.o objects/MAFTransferTable.o objects/TestTransferTable.o objects/FuelTables.o objects/FuelTables2.o objects/TimingTables.o objects/TimingTables2.o objects/TunableConfig.o objects/TunableConfig2.o objects/flashBurn.o
/usr/bin/m68hc11-ld: warning: cannot find entry symbol _start; defaulting to 0000c000
objects/Simple.o: In function `PrimaryRPMISR':
/home/fred/workspaces/home/freeems-vanilla/src/Simple.c:69: undefined reference to `_.tmp'
/home/fred/workspaces/home/freeems-vanilla/src/Simple.c:69: undefined reference to `_.z'
/home/fred/workspaces/home/freeems-vanilla/src/Simple.c:69: undefined reference to `_.xy'
/home/fred/workspaces/home/freeems-vanilla/src/Simple.c:69: undefined reference to `_.d1'
/home/fred/workspaces/home/freeems-vanilla/src/Simple.c:69: undefined reference to `_.d2'
/home/fred/workspaces/home/freeems-vanilla/src/Simple.c:69: undefined reference to `_.d3'
/home/fred/workspaces/home/freeems-vanilla/src/Simple.c:69: undefined reference to `_.d4'
/home/fred/workspaces/home/freeems-vanilla/src/Simple.c:69: undefined reference to `_.d5'
/home/fred/workspaces/home/freeems-vanilla/src/Simple.c:69: undefined reference to `_.d6'
and the list goes on

I can link from gcc, but I need to use an external linker that has special patches.

I'm not quite sure where I am going wrong.  Any input would be appreciated.
0
Comment
Question by:sean-keys
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 39

Accepted Solution

by:
noci earned 143 total points
ID: 33652630
I don't have specific knowledge of your platform (m86)..
But most of the time you also need some standard libraries like libc etc.
(you can specify them with -lc)

You might need more depending on the include files used.
0
 
LVL 34

Assisted Solution

by:Duncan Roe
Duncan Roe earned 144 total points
ID: 33652761
Please post line 69 of Simple.c.
As well as the libraries noci mentioned, if you are using ld instead of gcc then you will need to give it items like crtbegin.o and crtend.o or variants as typically found in /usr/lib/{gcc architecture}/{gcc revision}
Really I think you are better off to use gcc to to the loading - if you have loader options then you can pass them in with -Wl,{comma-separated option list} or multiple instances of -Wl,{ld option or option value}. I must confess I have only ever used the latter, although the documentation suggests to me that the former should work
0
 
LVL 34

Assisted Solution

by:Duncan Roe
Duncan Roe earned 144 total points
ID: 33652812
The -v flag to gcc used as a loader will show the ld command that is actually used
0
 

Author Comment

by:sean-keys
ID: 33654264
Ah yeah seems to work with -l /mygcclibs/....  

except  this except.....

mc9s12x-elf-ld: /usr/lib/gcc-lib/m68hc11/3.3.6-m68hc1x-20060122/m68hc12/mshort/crt1.o: Relocations in generic ELF (EM: 53)
/usr/lib/gcc-lib/m68hc11/3.3.6-m68hc1x-20060122/m68hc12/mshort/crt1.o: could not read symbols: File in wrong format
make: *** [output/freeems-0.1.1-SNAPSHOT-Simple.elf] Error 1
seank@mob-sean:/work/workspaceCDT/binutils-mc9s12x/build-testing/testing/f-mc9/freeems-vanilla/src$

So I need to recompile gcc using my binutils package correct?
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 33655279
Wasn't m68hc11-gcc built that way already? Is the output you posted from a run of m68hc11-gcc?
I wonder if there is another m68hc11 crt1.o somewhere that you should load manually - locate crt1.o|grep -w crt1 will show
0
 
LVL 22

Assisted Solution

by:NovaDenizen
NovaDenizen earned 213 total points
ID: 33663722
I don't have your m68hc11-gcc handy, so I'll use my gcc as an example of how to do this.

Say I have a file helloworld.c.  Normally I would just type "gcc -o helloworld helloworld.c" and everything is taken care of.  But I want to see the individual steps, so I use the "-v" flag too.
gcc -v -o helloworld helloworld.c

This produces lots of output, but you want to look for the compiler command, assembly command, and the link command.
The compiler command will look like this:
/path/to/cc <flags> helloworld.c <more flags> -o /var/tmp/blahblah.s
The assembly command will look something like this:
/path/to/as <flags> -o /var/tmp/blahblah.o /var/tmp/blahblah.s
The link command will look something like this:
/path/to/linker <flags> -o helloworld /path/to/crt1.o /path/to/crti.o <more crtXXX.o files here> /var/tmp/blahblah.o  <-L flags> <-l flags> /path/to/crtend.o /path/to/crtn.o

The compiler adds in those "crtxyz.o" files and -l flags to every c executable.  These are the C Run Time objects.  They wrap around your C program and are where the OS _really_ starts your program.  Some of these objects and libraries contain the symbols your program is missing.
0
 

Author Comment

by:sean-keys
ID: 33665633
That's just it crt1.o needs to be built with my new binutils. Which I have done, but now I get this error when I try to link more than one source file.

mc9$ make all
mc9s12x-elf-as --gstabs  -o main.o main.s
mc9s12x-elf-as --gstabs  -o xgate.o xgate.s
mc9s12x-elf-ld -m mc9s12xelfb --defsym _start=0 -o test.elf main.o xgate.o

 in bfd_default_compatible about to compare 31 and 31
 in bfd_default_compatible about to compare 31 and 31
mc9s12x-elf-ld: failed to merge target specific data of file xgate.o
 in bfd_default_compatible about to compare 31 and 31
 input was 31 output was 31make: *** [test.elf] Error 1

My printf code says they are of the same target, so I dont see what the problem is.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 22

Assisted Solution

by:NovaDenizen
NovaDenizen earned 213 total points
ID: 33666093
Try using the --no-warn-mismatch flag.  
0
 

Author Comment

by:sean-keys
ID: 33666366
That did work for linking two files generated with asm.  But when I try the gcc example I get

 m68hc11-gcc -mc9s12x -mshort -Wl,-m,mc9s12xelfb --no-warn-mismatch -Wl,-defsym,_io_ports=0x1000 -o test.elf main.o vectors.o
/usr/bin/mc9s12x-elf-ld: failed to merge target specific data of file main.o
/usr/bin/mc9s12x-elf-ld: failed to merge target specific data of file vectors.o
/usr/bin/mc9s12x-elf-ld: failed to merge target specific data of file /usr/lib/gcc-lib/m68hc11/3.3.6-m68hc1x-20060122/mshort/libgcc.a(_regs_min.o)
/usr/bin/mc9s12x-elf-ld: failed to merge target specific data of file /usr/lib/gcc-lib/m68hc11/3.3.6-m68hc1x-20060122/mshort/libgcc.a(_premain.o)
/usr/bin/mc9s12x-elf-ld: failed to merge target specific data of file /usr/lib/gcc-lib/m68hc11/3.3.6-m68hc1x-20060122/mshort/libgcc.a(__exit.o)
/usr/bin/mc9s12x-elf-ld: failed to merge target specific data of file /usr/lib/gcc-lib/m68hc11/3.3.6-m68hc1x-20060122/mshort/libgcc.a(_map_data.o)
/usr/bin/mc9s12x-elf-ld: failed to merge target specific data of file /usr/lib/gcc-lib/m68hc11/3.3.6-m68hc1x-20060122/mshort/libgcc.a(_init_bss.o)

 in bfd_default_compatible about to compare 31 and 31
 in bfd_default_compatible about to compare 31 and 31
 in bfd_default_compatible about to compare 31 and 31
 in bfd_default_compatible about to compare 31 and 31
 in bfd_default_compatible about to compare 31 and 31
 input was 31 output was 31
 in bfd_default_compatible about to compare 31 and 31
 input was 31 output was 31
 in bfd_default_compatible about to compare 31 and 31
 input was 31 output was 31
 in bfd_default_compatible about to compare 31 and 31
 input was 31 output was 31
 in bfd_default_compatible about to compare 31 and 31
 input was 31 output was 31
 in bfd_default_compatible about to compare 31 and 31
 input was 31 output was 31
 in bfd_default_compatible about to compare 31 and 31
 input was 31 output was 31collect2: ld returned 1 exit status
0
 
LVL 22

Assisted Solution

by:NovaDenizen
NovaDenizen earned 213 total points
ID: 33666526
You need to tell gcc that the --no-warn-mismatch is for the linker.  You do that by using "-Xlinker --no-warn-mismatch"
0
 

Author Comment

by:sean-keys
ID: 33668651
That did allow my sample project to build.  But my main project which requires newlib fails.  So i figured I could just recompile newlib.  It builds most of it except the last section.

Configured with: ../src/configure --prefix=/usr --infodir=/usr/share/info --mandir=/usr/share/man --with-gnu-as --with-gnu-ld --enable-nls --without-inclu$
Thread model: single
gcc version 3.3.6-m68hc1x-20060122
 /usr/lib/gcc-lib/m68hc11/3.3.6-m68hc1x-20060122/collect2 -m m68hc11elf -relax /usr/lib/gcc-lib/m68hc11/3.3.6-m68hc1x-20060122/crt1.o -L/usr/lib/gcc-lib/m$
/usr/lib/gcc-lib/m68hc11/3.3.6-m68hc1x-20060122/crt1.o:(.install4+0x1): undefined reference to `main'

I wonder if I my "mc9s12x-elf-ld: failed to merge target specific data of file" problem is really a problem.,,,,
0
 
LVL 39

Assisted Solution

by:noci
noci earned 143 total points
ID: 33669506
A shared library has no function main(). ==> you need a different start module from crt1.o
(that's for a main program)
The main() function is reserved for the entry point of any program, so it cannot be in a library.

Check out what gcc uses on you platform for linking a shared object. for a shared object you add
 "-shared"  to your gcc command line.
0
 
LVL 22

Expert Comment

by:NovaDenizen
ID: 33673735
What noci said.  Figure out how to compile and link a library using a stock compiler, do the same test with your compiler to make sure you have it down, then use -v to see the flags and other hidden stuff.
0
 

Author Closing Comment

by:sean-keys
ID: 33694063
Thanks guys, I was able to use the information in this thread to get it working!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now