Kamilos
Kamilos

Reputation: 177

Compile linker script in cmake - cannot find object file (embedded project)

My problem is that I want to do a cmake file from normal makefile from this project: https://github.com/brianwiddas/pi-baremetal

I create CMakeLists file, but I'm having trouble with linking the project. After make command I have an error like this:

Linking C executable rpi-0.1.elf
/usr/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/bin/ld: cannot find start.o
collect2: error: ld returned 1 exit status
make[2]: *** [rpi-0.1.elf] Błąd 1
make[1]: *** [CMakeFiles/rpi.dir/all] Błąd 2

Linker code fragment:

MEMORY
{
    initsys : org = 0x8000, len = 1M
    kernel : org = 0xf0000000, len = 1M
    data : org = 0xc0000000, len = 1M
}
SECTIONS
{
    /* Initial section, loads/runs at 0x8000 */
    .init : {
        start.o(.text* .data* .bss* .rodata*)
        initsys.o (.text* .data* .bss* .rodata*)
    } >initsys

    //the rest of code in SECTION

}

I realised that cmake create object file with ".c.obj/.s.obj" extension, compare with this makefile with ".o" extension, I change it in linker code, but now the error is:

cannot find start.s.obj

Now I dont know what is wrong. Maybe because in makefile project the source code file and object file is in the same folder. Cmake create object file in another folder, in my case:

source files folder - ../Projekt/code
object files folder - ../Projekt/build/CMakeFiles/rpi.dir/code

Maybe the linker script do not see the object files, because they are in another folder. The build folder is created automatically by cmake.

This is my CMakeLists.txt file:

## Minimal cmake version
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

## Project name
project(rpi C CXX ASM)

## Project version
set(${PROJECT_NAME}_version "0.1")

## Project options
option(DISABLE_NEWLIB_SUPPLIED_SYSCALLS "Disables pre-defined libraries for ARM" ON)

## Compilation options
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O6 -nostdinc -ffreestanding -marm -mcpu=arm1176jzf-s")

## Creation of the necessary definitions
if (DISABLE_NEWLIB_SUPPLIED_SYSCALLS)
    add_definitions(-DDISABLE_NEWLIB_SUPPLIED_SYSCALLS)
endif (DISABLE_NEWLIB_SUPPLIED_SYSCALLS)

## Assembler source code
set(ASM_SOURCE_FILES ${PROJECT_SOURCE_DIR}/code/start.s)

## C source code
set(C_SOURCE_FILES ${PROJECT_SOURCE_DIR}/code/atags.c
           ${PROJECT_SOURCE_DIR}/code/atags.h
           ${PROJECT_SOURCE_DIR}/code/barrier.h
           ${PROJECT_SOURCE_DIR}/code/divby0.c
           ${PROJECT_SOURCE_DIR}/code/framebuffer.c
           ${PROJECT_SOURCE_DIR}/code/framebuffer.h
           ${PROJECT_SOURCE_DIR}/code/interrupts.c
           ${PROJECT_SOURCE_DIR}/code/interrupts.h
           ${PROJECT_SOURCE_DIR}/code/led.c
           ${PROJECT_SOURCE_DIR}/code/led.h
           ${PROJECT_SOURCE_DIR}/code/initsys.c
           ${PROJECT_SOURCE_DIR}/code/mailbox.c
           ${PROJECT_SOURCE_DIR}/code/mailbox.h
           ${PROJECT_SOURCE_DIR}/code/main.c
           ${PROJECT_SOURCE_DIR}/code/memory.c
           ${PROJECT_SOURCE_DIR}/code/memory.h
           ${PROJECT_SOURCE_DIR}/code/memutils.c
           ${PROJECT_SOURCE_DIR}/code/memutils.h
           ${PROJECT_SOURCE_DIR}/code/teletext.h
           ${PROJECT_SOURCE_DIR}/code/textutils.c
           ${PROJECT_SOURCE_DIR}/code/textutils.h)

## Additional files
set(OTHER_FILES ${PROJECT_SOURCE_DIR}/code/linker.ld)

## Create an executable binary files
add_executable(${PROJECT_NAME} ${ASM_SOURCE_FILES} ${C_SOURCE_FILES})

## Settings resulting executable file
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "${PROJECT_NAME}-${${PROJECT_NAME}_version}.elf")
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-Wl,-Map,${CMAKE_BINARY_DIR}/${PROJECT_NAME}-${${PROJECT_NAME}_version}.map -T ${PROJECT_SOURCE_DIR}/code/linker.ld -nostdlib -nostartfiles -gc-sections")

## Creates a kernel image
add_custom_command(TARGET ${PROJECT_NAME} COMMAND ${CROSS_COMPILER_PREFIX}objcopy ${CMAKE_BINARY_DIR}/${PROJECT_NAME}-${${PROJECT_NAME}_version}.elf -O binary ${CMAKE_BINARY_DIR}/${PROJECT_NAME}-${${PROJECT_NAME}_version}.img)

and toolchain-raspberry-pi.cmake file:

## Determination of the operating system on which the program is designed
set(CMAKE_SYSTEM_NAME Generic)

## The term architecture on which the program is established
set(${PROJECT_NAME}_architecture raspberry-pi)

## Prefix tools used for cross-compiling
set(CROSS_COMPILER_PREFIX arm-none-eabi-)

## Compilers that will be used.
include (CMakeForceCompiler)
cmake_force_c_compiler(${CROSS_COMPILER_PREFIX}gcc GNU)
cmake_force_cxx_compiler(${CROSS_COMPILER_PREFIX}g++ GNU)

How to fix this error and why cmake do not create object files with ".o" extensions and in the same folder as source code? Please help.

Solution:

In linker script I add * before start.s.obj/initsys.c.obj like this:

*start.s.obj (.text* .data* .bss* .rodata*)
*initsys.c.obj (.text* .data* .bss* .rodata*)

Upvotes: 1

Views: 3295

Answers (1)

theadnangondal
theadnangondal

Reputation: 1664

can you try *start*.o(.text* .data* .bss* .rodata*) in linker file .

PS: I was unable to compile your project. I guess you are cross compiling with arm-none-eabi-* toolchain. how are you overriding the default compilers ?

Upvotes: 1

Related Questions