This is an archive of the discontinued LLVM Phabricator instance.

[MC] report error instead of assertion for non-zero initializer in .bss section
ClosedPublic

Authored by weimingz on Jun 18 2014, 3:03 PM.

Details

Summary

User may initialize a variable with non-zero value and put it into .bss section by mistake.

E.g. : int a __attribute__((section(".bss"))) = 2; // mistakenly initialized to 2

This patch converts an assertion to error report for better user experience.

Diff Detail

Repository
rL LLVM

Event Timeline

weimingz updated this revision to Diff 10591.Jun 18 2014, 3:03 PM
weimingz retitled this revision from to [MC] report error instead of assertion for non-zero initializer in .bss section.
weimingz updated this object.
weimingz edited the test plan for this revision. (Show Details)
weimingz added a subscriber: Unknown Object (MLST).
rnk added a subscriber: rnk.Jun 18 2014, 3:37 PM

This seems like an OK diagnostic for now in LLVM, but eventually we will need one in clang.

lib/MC/MCAssembler.cpp
789–790 ↗(On Diff #10591)

This creates a StringRef that points to a destroyed std::string.

weimingz updated this revision to Diff 10600.Jun 18 2014, 5:22 PM
rafael added inline comments.
lib/MC/MCAssembler.cpp
788 ↗(On Diff #10600)

You can probably use a dyn_cast.

791 ↗(On Diff #10600)

How about just having two report_fatal_error calls? The ELF one could be

report_fatal_error("non-zero initializer found in " + Section->getSectionName())

and the non-elf one

report_fatal_error("non-zero initializer found in an virtual section");

792 ↗(On Diff #10600)

report_fatal_error doesn't return, so you don't need this.

weimingz updated this revision to Diff 10705.Jun 20 2014, 1:46 PM

Hi Rafael,

Thanks for reviewing. The new patch is based on your suggestion.

rafael added inline comments.Jun 20 2014, 2:03 PM
lib/MC/MCAssembler.cpp
787 ↗(On Diff #10705)

Sorry, what I mean is that you can use the dyn_cast in here. It will return a MCSectionELF or null. So instead of

if (check)

cast

you have

if (auto *ELFSection = dyn_cast...)

Use ELFSection
weimingz updated this revision to Diff 10708.Jun 20 2014, 4:53 PM

Hi Rafael,

Sorry I didn't get it. Now it makes sense. :D

rafael accepted this revision.Jun 20 2014, 5:04 PM
rafael added a reviewer: rafael.

LGTM with a nit.

lib/MC/MCAssembler.cpp
787 ↗(On Diff #10708)

nit:

use 'auto *' to make it clear that it is a pointer.

This revision is now accepted and ready to land.Jun 20 2014, 5:04 PM
In D4199#3, @rnk wrote:

This seems like an OK diagnostic for now in LLVM, but eventually we will need one in clang.

It's good have a check in clang with more detailed info. (line no. etc).

In D4199#13, @rafael wrote:

LGTM with a nit.

Thanks! I'll commit with the change.

lib/MC/MCAssembler.cpp
789–790 ↗(On Diff #10591)

Thanks. Will fix.

weimingz closed this revision.Jun 21 2014, 5:42 PM
weimingz updated this revision to Diff 10724.

Closed by commit rL211455 (authored by @weimingz).