1

I'm using ATMEL Studio 6.2 and its toolchain with avr-gcc (avr8-gnu-toolchain). I have a variable that needs to be placed in flash (PROGMEM) and I declare it as a global:

static const uint16_t gPrgLen PROGMEM __attribute__((used)) = 0;

The compiler doesn't complain and the linker doesn't complain, but when I open the .lss file, there is no gPrgLen to be found. In the .map file we can see that it has been listed under "discarded input sections"

Discarded input sections
.progmem.data.gPrgLen    0x00000000    0x2    Boot.o

It is built as a release, but a debug build gives the same result. How can I force the linker to include this variable in the *(.progmem*) section?

EDIT
Added static but still the same result.

Here is the linker part:

# All Target
all: $(OUTPUT_FILE_PATH) $(ADDITIONAL_DEPENDENCIES)

$(OUTPUT_FILE_PATH): $(OBJS) $(USER_OBJS) $(OUTPUT_FILE_DEP) $(LIB_DEP)
@echo Building target: $@
@echo Invoking: AVR/GNU Linker : 4.8.1
$(QUOTE)C:\Program Files (x86)\Atmel\Atmel Toolchain\AVR8 GCC\Native\3.4.1056\avr8-gnu-toolchain\bin\avr-gcc.exe$(QUOTE) -o$(OUTPUT_FILE_PATH_AS_ARGS) $(OBJS_AS_ARGS) $(USER_OBJS) $(LIBS) -Wl,-Map="Boot.map" -Wl,--start-group -Wl,-lm  -Wl,--end-group -Wl,--gc-sections -Wl,-section-start=.text=0xf800  -mmcu=at90usb647  
@echo Finished building target: $@
"C:\Program Files (x86)\Atmel\Atmel Toolchain\AVR8 GCC\Native\3.4.1056\avr8-gnu-toolchain\bin\avr-objcopy.exe" -O ihex -R .eeprom -R .fuse -R .lock -R .signature -R .user_signatures  "Boot.elf" "Boot.hex"
"C:\Program Files (x86)\Atmel\Atmel Toolchain\AVR8 GCC\Native\3.4.1056\avr8-gnu-toolchain\bin\avr-objcopy.exe" -j .eeprom  --set-section-flags=.eeprom=alloc,load --change-section-lma .eeprom=0  --no-change-warnings -O ihex "Boot.elf" "Boot.eep" || exit 0
"C:\Program Files (x86)\Atmel\Atmel Toolchain\AVR8 GCC\Native\3.4.1056\avr8-gnu-toolchain\bin\avr-objdump.exe" -h -S "Boot.elf" > "Boot.lss"
"C:\Program Files (x86)\Atmel\Atmel Toolchain\AVR8 GCC\Native\3.4.1056\avr8-gnu-toolchain\bin\avr-objcopy.exe" -O srec -R .eeprom -R .fuse -R .lock -R .signature -R .user_signatures "Boot.elf" "Boot.srec"
"C:\Program Files (x86)\Atmel\Atmel Toolchain\AVR8 GCC\Native\3.4.1056\avr8-gnu-toolchain\bin\avr-size.exe" "Boot.elf"
4

1 回答 1

0

奇怪的__attribute__((used))是不起作用。两个建议试试。

首先,将变量从更改staticvolatile(或只是添加volatile)。这可能会阻止它被优化掉。

如果这不起作用,您可以在链接器中添加一行以使其“[p]retend the symbol symbol is undefined, to force links of library modules to define it”(GCC Link Options)。这是通过-u symbol或完成的--undefined=symbol

要将其添加到 Atmel Studio 项目文件,请转到 Toolchain -> AVR/GNU Linker -> Miscellaneous。然后在其他链接器标志中添加--undefined=gPrgLen.

我用它来将修订/编译时信息嵌入到未使用的十六进制文件中。这样我就可以从设备中检索内存并知道它是在什么条件下构建的(主要用于在原型设计和初始固件调试期间跟踪更改)。我的 main.c 文件有一个看起来像const char codeCompileDetails[] PROGMEM = "company_name-" __DATE__ "-" __TIME__;. 再加上--undefined=codeCompileDetails,该数据(这里包括编译代码的日期和时间)总是使其成为可执行文件。

于 2017-07-06T18:41:12.867 回答