[CMake] Linking twice for CRC32

2019-10-11 Thread Vincent van Beveren

Hello  everyone,

For integrating the CRC to an ELF I believe I will need to link twice. 
First to create a binary on which to caclulate the CRC, and using a 
second pass to actually put the CRC into a the symbol at the last part 
of the RAM. Using the pointers by Eric  Noulard I have constructed the 
following CMake code, which links the file, exports a binary BIN file 
(the actual ROM image, and then calculates the CRC into a file called 
$.crc32.


# Exprimental CRC32 support
add_library(clb_v2_dom.objs OBJECT ${F_GEN} ${F__DOM})
target_compile_definitions(clb_v2_dom.objs PUBLIC -DDOM -DCLBV2 )
add_executable(clb_v2_dom.elf $)
add_custom_command(TARGET clb_v2_dom.elf POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} $ ${SECTIONS} 
-O binary $.bin
    COMMAND ${Python_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tools/crc32.py 
$.bin > $.crc32

)

with linker flags defined as

# Bring up the linker
add_link_options(
    -nostartfiles
    -nodefaultlibs
    -Wl,--gc-sections
    -nostdlib
    -T ${CMAKE_SOURCE_DIR}/${P_SRC}/romram.ld
    -Wl,--defsym=CRC_VALUE=0
    -Wl,--defsym=_start=0
    )

However, the last part is to link the file again with *exactly* the same 
invocation as the original linking, except for the 
'-Wl,--defsym=CRC_VALUE=<>' instead of 0.


Can anyone give me points on how to achieve this goal?

Kind regards,
Vincent

--
National Institute for Subatomic Physics Nikhef
Department of Computer Technology
Science Park 105
1098 XG AMSTERDAM

tel.  : +31 (0)20 592 2032
e-mail: v.van.beve...@nikhef.nl
site  : http://www.nikhef.nl/~vincentb

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake


Re: [CMake] Adding CRC32 to ELF

2019-10-07 Thread Vincent van Beveren

Hi Eric,

Thanks for your prompt reply.

I have been able to follow your steps up until the item, so thanks!

 COMMAND ${CMAKE_C_COMPILER} $ 
--symbol $.crc32  $


I ran into two challenges:
- The argument '$.crc32' should be the 
actual content of the file, and not the file itself, i.e. the calculated 
CRC value.  How to do this?
- As far as I can see, non of the previously defined linker flags are 
added to this command. Of course I can do this, but it would mean I need 
to define things twice.


Cheers,
Vincent

On 7-10-2019 at 15:55 wrote Eric Noulard:



Le lun. 7 oct. 2019 à 14:41, Vincent van Beveren 
mailto:v.van.beve...@nikhef.nl>> a écrit :


Hello everyone,

I'm setting up a new build targeting an embedded platform (LM32) and
trying to integrate a CRC32 into the resulting ELF. I have previously
done this using plain Makefiles with the following steps (pseudo
code):

target.elf: 
    link  --symbol CRC_VALUE=0 intermediate.elf
    objcopy --binary intermediate.elf intermediate.bin
    link  --symbol CRC_VALUE=`crc32 intermediate.bin`
target.elf
    rm intermediate.bin

Now I would like to achieve the same thing with CMake. I'm using
add_executable with multiple targets. The end of my CMakeLists.txt
looks
like this, where F_GEN contains generic source files and F__* source
files specific for that variant of the build:

# [...defining of cross compiler, and source files, some compile
flags,
etc...]
# Educate the linker
add_link_options(
 -nostartfiles
 -nodefaultlibs
 -nostdlib
 -Wl,--gc-sections
 -T ${CMAKE_SOURCE_DIR}/${P_SRC}/romram.ld
 -Wl,--defsym=CRC_VALUE=0
 -Wl,--defsym=_start=0
 )

# DOM v2 target
add_executable( clb_v2_dom.elf ${F_GEN} ${F__DOM} )
target_compile_definitions(clb_v2_dom.elf PUBLIC -DDOM -DCLBV2 )

# BASE v2 target
add_executable( clb_v2_base.elf ${F_GEN} ${F__BASE} )
# TODO migrate define 'DUBASE' -> 'BASE'
target_compile_definitions(clb_v2_base.elf PUBLIC -DDUBASE -DBASE
-DCLBV2)

# Golden v2 target
add_executable( clb_v2_golden.elf ${F_GEN} ${F__GLD} )
target_compile_definitions( clb_v2_golden.elf PUBLIC -DGOLDEN
-DCLBV2 )

==

As you can see CRC_VALUE is now simply defined 0 for every target.
Which
works well for compiling but during runtime poses a problem to the
ROM
verification procedure. What would a 'proper' way be of adding a
CRC to
an ELF file be using CMake, and be different for each target. Any
help
is welcome!


I would try to add a set of custom command as POST_BUILD event.
https://cmake.org/cmake/help/v3.15/command/add_custom_command.html#build-events

add_custom_command(TARGET clb_v2_base.elf POST_BUILD
 COMMAND crc32 $ 
> $.crc32
                                   COMMAND ${CMAKE_C_COMPILER} 
--symbol $.crc32 
 $

                                  )

I don't know if the linker can read the computed crc32 from the 
previously generated file but you get the idea.
Moreover if you want to easily collect objects used for linking a 
target you may need to use an intermediate

OBJECT library in order to be able to retrieve $
see: 
https://cmake.org/cmake/help/latest/command/add_library.html#object-libraries

i.e. replace:
add_executable( clb_v2_dom.elf ${F_GEN} ${F__DOM} )

by
add_library(clb_v2_dom.objs OBJECT ${F_GEN} ${F__DOM})
add_executable(clb_v2_dom.elf $)

and then:

add_custom_command(TARGET clb_v2_dom.elf POST_BUILD
 COMMAND crc32 $ 
> $.crc32
                                   COMMAND 
${CMAKE_C_COMPILER} $ 
--symbol $.crc32 
 $

                                  )

If this looks ok to you I would then write my own

lm32_add_executable that would wraps this up in order to be called as:

lm32_add_executable(EXECUTABLE clb_v2_dom.elf
                                 SOURCES ${F_GEN} ${F__DOM})




Kind regards,
Vincent



-- 
National Institute for Subatomic Physics Nikhef

Department of Computer Technology
Science Park 105
1098 XG AMSTERDAM

tel.  : +31 (0)20 592 2032
e-mail: v.van.beve...@nikhef.nl <mailto:v.van.beve...@nikhef.nl>
site  : http://www.nikhef.nl/~vincentb

-- 


Powered by www.kitware.com <http://www.kitware.com>

Please keep messages on-topic and check the CMake FAQ at:
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community.
For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake



--
E

[CMake] Adding CRC32 to ELF

2019-10-07 Thread Vincent van Beveren

Hello everyone,

I'm setting up a new build targeting an embedded platform (LM32) and 
trying to integrate a CRC32 into the resulting ELF. I have previously 
done this using plain Makefiles with the following steps (pseudo code):


target.elf: 
   link  --symbol CRC_VALUE=0 intermediate.elf
   objcopy --binary intermediate.elf intermediate.bin
   link  --symbol CRC_VALUE=`crc32 intermediate.bin` target.elf
   rm intermediate.bin

Now I would like to achieve the same thing with CMake. I'm using 
add_executable with multiple targets. The end of my CMakeLists.txt looks 
like this, where F_GEN contains generic source files and F__* source 
files specific for that variant of the build:


# [...defining of cross compiler, and source files, some compile flags, 
etc...]

# Educate the linker
add_link_options(
    -nostartfiles
    -nodefaultlibs
    -nostdlib
    -Wl,--gc-sections
    -T ${CMAKE_SOURCE_DIR}/${P_SRC}/romram.ld
    -Wl,--defsym=CRC_VALUE=0
    -Wl,--defsym=_start=0
    )

# DOM v2 target
add_executable( clb_v2_dom.elf ${F_GEN} ${F__DOM} )
target_compile_definitions(clb_v2_dom.elf PUBLIC -DDOM -DCLBV2 )

# BASE v2 target
add_executable( clb_v2_base.elf ${F_GEN} ${F__BASE} )
# TODO migrate define 'DUBASE' -> 'BASE'
target_compile_definitions(clb_v2_base.elf PUBLIC -DDUBASE -DBASE -DCLBV2)

# Golden v2 target
add_executable( clb_v2_golden.elf ${F_GEN} ${F__GLD} )
target_compile_definitions( clb_v2_golden.elf PUBLIC -DGOLDEN -DCLBV2 )

==

As you can see CRC_VALUE is now simply defined 0 for every target. Which 
works well for compiling but during runtime poses a problem to the ROM 
verification procedure. What would a 'proper' way be of adding a CRC to 
an ELF file be using CMake, and be different for each target. Any help 
is welcome!


Kind regards,
Vincent



--
National Institute for Subatomic Physics Nikhef
Department of Computer Technology
Science Park 105
1098 XG AMSTERDAM

tel.  : +31 (0)20 592 2032
e-mail: v.van.beve...@nikhef.nl
site  : http://www.nikhef.nl/~vincentb

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake