{"action":"created","issue":{"url":"https://api.github.com/repos/0xc0170/kinetis_klxx_gcc/issues/1","labels_url":"https://api.github.com/repos/0xc0170/kinetis_klxx_gcc/issues/1/labels{/name}","comments_url":"https://api.github.com/repos/0xc0170/kinetis_klxx_gcc/issues/1/comments","events_url":"https://api.github.com/repos/0xc0170/kinetis_klxx_gcc/issues/1/events","html_url":"https://github.com/0xc0170/kinetis_klxx_gcc/issues/1","id":58627293,"number":1,"title":"missing alignment in linker script","user":{"login":"prof7bit","id":1401589,"avatar_url":"https://avatars.githubusercontent.com/u/1401589?v=3","gravatar_id":"","url":"https://api.github.com/users/prof7bit","html_url":"https://github.com/prof7bit","followers_url":"https://api.github.com/users/prof7bit/followers","following_url":"https://api.github.com/users/prof7bit/following{/other_user}","gists_url":"https://api.github.com/users/prof7bit/gists{/gist_id}","starred_url":"https://api.github.com/users/prof7bit/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/prof7bit/subscriptions","organizations_url":"https://api.github.com/users/prof7bit/orgs","repos_url":"https://api.github.com/users/prof7bit/repos","events_url":"https://api.github.com/users/prof7bit/events{/privacy}","received_events_url":"https://api.github.com/users/prof7bit/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/0xc0170/kinetis_klxx_gcc/labels/bug","name":"bug","color":"fc2929"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-02-23T18:29:05Z","updated_at":"2015-02-23T22:18:54Z","closed_at":null,"body":"Hello,\r\n\r\nthank you for these files, they were incredibly helpful for me already!\r\n\r\nBut there is a problem: It only happens when using -nostartfiles or -nostdlib.\r\n\r\nI began experimenting with the linker options and leaving away the gcc generated standard startup code and providing my own implementation of _start instead. This went fine until I had some global variables to initialize with the flash_to_ram loop. It hard-faulted on the very first ldr instruction in the freescale provided startup_xxx.s file.\r\n\r\nUpon closer inspection of what has happened I observed that my data was not aligned in the flash anymore and according to what I have read this will result in a hardfault.\r\n\r\nThis is how the last 4 bytes look when using the original linker script (they should be an unsigned long number 42), this longword is not aligned at 4-byte boundary:\r\n\r\n![_align_wrong](https://cloud.githubusercontent.com/assets/1401589/6333813/758f0898-bb90-11e4-82df-ea2d6d3c88c5.png)\r\n\r\nand this is how it looked after throwing in another ALIGN(4) into the linker script at the position I thought should be correct (see patch below), now it is aligned. This is also what somehow magically happens when *not* using the -nostartfiles or -nostdlib options in gcc, it seems gcc will change these symbos on its own by default, so the bug in the original linker script had no effect at all with standard compilation options.\r\n\r\n![_align_correct](https://cloud.githubusercontent.com/assets/1401589/6333832/9a1c95f4-bb90-11e4-9d3e-edb50816e333.png)\r\n\r\nNow here is what I did: \r\n\r\nI don't know much about linker scripts, this is just what I intuitively believed to be correct, I just inserted another ALIGN(4) in front of the __exidx_end symbol:\r\n\r\n```\r\ndiff -u cmsis/MKL05Z4/MKL05Z4_old.ld cmsis/MKL05Z4/MKL05Z4.ld \r\n--- cmsis/MKL05Z4/MKL05Z4_old.ld        2015-02-23 18:40:22.734195960 +0100\r\n+++ cmsis/MKL05Z4/MKL05Z4.ld    2015-02-23 18:57:31.905617614 +0100\r\n@@ -97,6 +97,8 @@\r\n     {\r\n         *(.ARM.exidx* .gnu.linkonce.armexidx.*)\r\n     } > FLASH\r\n+    \r\n+    . = ALIGN(4);\r\n     __exidx_end = .;\r\n     \r\n     __etext = .;\r\n\r\n```\r\nNow the data is aligned properly."},"comment":{"url":"https://api.github.com/repos/0xc0170/kinetis_klxx_gcc/issues/comments/75649690","html_url":"https://github.com/0xc0170/kinetis_klxx_gcc/issues/1#issuecomment-75649690","issue_url":"https://api.github.com/repos/0xc0170/kinetis_klxx_gcc/issues/1","id":75649690,"user":{"login":"0xc0170","id":2963726,"avatar_url":"https://avatars.githubusercontent.com/u/2963726?v=3","gravatar_id":"","url":"https://api.github.com/users/0xc0170","html_url":"https://github.com/0xc0170","followers_url":"https://api.github.com/users/0xc0170/followers","following_url":"https://api.github.com/users/0xc0170/following{/other_user}","gists_url":"https://api.github.com/users/0xc0170/gists{/gist_id}","starred_url":"https://api.github.com/users/0xc0170/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/0xc0170/subscriptions","organizations_url":"https://api.github.com/users/0xc0170/orgs","repos_url":"https://api.github.com/users/0xc0170/repos","events_url":"https://api.github.com/users/0xc0170/events{/privacy}","received_events_url":"https://api.github.com/users/0xc0170/received_events","type":"User","site_admin":false},"created_at":"2015-02-23T22:18:54Z","updated_at":"2015-02-23T22:18:54Z","body":"Hi, thanks for reporting. Looks like I missed the alignment for vtable and data. Will you send the pull request, all the ld need to be fixed.\r\n\r\nI propose this:\r\n\r\n```\r\n    .data : AT (__etext)\r\n    {\r\n         . = ALIGN(4); // <-----added\r\n        __data_start__ = .;\r\n        *(vtable)\r\n        *(.data*)\r\n\r\n        . = ALIGN(4);\r\n        /* preinit data */\r\n        PROVIDE_HIDDEN (__preinit_array_start = .);\r\n        KEEP(*(.preinit_array))\r\n        PROVIDE_HIDDEN (__preinit_array_end = .);\r\n\r\n        . = ALIGN(4);\r\n        /* init data */\r\n        PROVIDE_HIDDEN (__init_array_start = .);\r\n        KEEP(*(SORT(.init_array.*)))\r\n        KEEP(*(.init_array))\r\n        PROVIDE_HIDDEN (__init_array_end = .);\r\n\r\n\r\n        . = ALIGN(4);\r\n        /* finit data */\r\n        PROVIDE_HIDDEN (__fini_array_start = .);\r\n        KEEP(*(SORT(.fini_array.*)))\r\n        KEEP(*(.fini_array))\r\n        PROVIDE_HIDDEN (__fini_array_end = .);\r\n\r\n        . = ALIGN(4);\r\n        /* All data end */\r\n        __data_end__ = .;\r\n\r\n    } > RAM\r\n```"}}