[CMAKE] Ensure the INIT section is placed at the end of a module. CORE-14683
authorThomas Faber <thomas.faber@reactos.org>
Sat, 29 Jun 2019 17:24:28 +0000 (19:24 +0200)
committerThomas Faber <thomas.faber@reactos.org>
Sun, 7 Jul 2019 06:18:10 +0000 (08:18 +0200)
For MSVC, marking the section as discardable will do this automatically.
For GCC, we use a linker script that places it after the .reloc section
(which should be the last "real" section, check ld --verbose output for the
default linker script).

This fixes what seems to be a regression from r55835 (!).

ntoskrnl/CMakeLists.txt
sdk/cmake/gcc.cmake
sdk/cmake/init-section.lds [new file with mode: 0644]
sdk/cmake/msvc.cmake

index 3111893..5f2ac87 100644 (file)
@@ -31,12 +31,14 @@ set_subsystem(ntoskrnl native)
 if(MSVC)
     set_image_base(ntoskrnl 0x00400000)
     add_target_link_flags(ntoskrnl "/SECTION:.rsrc,!DP") #Accessed from bugcheck code
+    add_target_link_flags(ntoskrnl "/SECTION:INIT,D")
 else()
     if(GDB)
         set_image_base(ntoskrnl 0x00800000)
     else()
         set_image_base(ntoskrnl 0x80800000)
     endif()
+    add_linker_script(ntoskrnl ${REACTOS_SOURCE_DIR}/sdk/cmake/init-section.lds)
 endif()
 
 target_link_libraries(ntoskrnl cportlib csq ${PSEH_LIB} cmlib ntlsalib rtl ${ROSSYM_LIB} libcntpr wdmguid ioevent)
index f5c1ec1..c4c741f 100644 (file)
@@ -318,6 +318,7 @@ function(set_module_type_toolchain MODULE TYPE)
         if(${TYPE} STREQUAL "wdmdriver")
             add_target_link_flags(${MODULE} "-Wl,--wdmdriver")
         endif()
+        add_linker_script(${MODULE} ${REACTOS_SOURCE_DIR}/sdk/cmake/init-section.lds)
     endif()
     
     if(STACK_PROTECTOR)
diff --git a/sdk/cmake/init-section.lds b/sdk/cmake/init-section.lds
new file mode 100644 (file)
index 0000000..2b81515
--- /dev/null
@@ -0,0 +1,11 @@
+/* Make sure the INIT section is at the end of the module so we can reclaim the space */
+SECTIONS
+{
+  INIT BLOCK(__section_alignment__) :
+  {
+    __init_start__ = . ;
+    *(INIT)
+    __init_end__ = . ;
+  }
+}
+INSERT AFTER .reloc;
index f7dc154..44b7277 100644 (file)
@@ -327,9 +327,9 @@ function(set_module_type_toolchain MODULE TYPE)
         add_target_link_flags(${MODULE} "/DLL")
     elseif(${TYPE} STREQUAL "kernelmodedriver")
         # Disable linker warning 4078 (multiple sections found with different attributes) for INIT section use
-        add_target_link_flags(${MODULE} "/DRIVER /IGNORE:4078")
+        add_target_link_flags(${MODULE} "/DRIVER /IGNORE:4078 /SECTION:INIT,D")
     elseif(${TYPE} STREQUAL "wdmdriver")
-        add_target_link_flags(${MODULE} "/DRIVER:WDM /IGNORE:4078")
+        add_target_link_flags(${MODULE} "/DRIVER:WDM /IGNORE:4078 /SECTION:INIT,D")
     endif()
 
     if(RUNTIME_CHECKS)