Skip to content

rustc puts zero-initalized structs in .data instead of .bss if they contain padding #41315

Closed
@whitequark

Description

@whitequark

E.g.:

const DMA_BUFFER_SIZE: usize = 8 * 1024 * 1024;

struct DmaRecorder {
    active:   bool,
    buffer:   [u8; DMA_BUFFER_SIZE],
    data_len: usize
}

static mut DMA_RECORDER: DmaRecorder = DmaRecorder {
    active:   false,
    buffer:   [0; DMA_BUFFER_SIZE],
    data_len: 0
};
$ size -Ax libt.rlib
t.0.o   (ex libt.rlib):
section                                             size   addr
.text                                                0x0    0x0
.text._ZN4drop17h735fab7fd9067161E                   0x6    0x0
.data._ZN1t12DMA_RECORDER17h7409bdfe01c28df8E   0x800010    0x0
.note.GNU-stack                                      0x0    0x0
.eh_frame                                           0x30    0x0
Total                                           0x800046

(and this results in a 8MB file, etc).

Note that this works:

const DMA_BUFFER_SIZE: usize = 8 * 1024 * 1024;

struct DmaRecorder {
    buffer:   [u8; DMA_BUFFER_SIZE],
    data_len: usize
}

static mut DMA_RECORDER: DmaRecorder = DmaRecorder {
    buffer:   [0; DMA_BUFFER_SIZE],
    data_len: 0
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-codegenArea: Code generationC-enhancementCategory: An issue proposing an enhancement or a PR with one.I-slowIssue: Problems and improvements with respect to performance of generated code.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions