public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/94134] New: pdp11-aout puts initial variable into .text section rather than .data
@ 2020-03-11  6:52 casner at acm dot org
  2020-03-11  7:05 ` [Bug target/94134] " casner at acm dot org
                   ` (16 more replies)
  0 siblings, 17 replies; 18+ messages in thread
From: casner at acm dot org @ 2020-03-11  6:52 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94134

            Bug ID: 94134
           Summary: pdp11-aout puts initial variable into .text section
                    rather than .data
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: casner at acm dot org
  Target Milestone: ---

The assembler output for a C program compiled for the pdp11-aout target begins
with a .text directive.  If that program begins with a variable that is not
initialized to a nonzero value, that variable will be left if the .text section
rather than being allocated to the .data section.  If the first variable is
initialized to a nonzero value, then a .data directive is inserted after the
.text directive to switch sections.  This bug matters for formats where the
text section is read-only or with split instruction and data spaces.

I selected component target, but I don't know if this problem is specific to
the pdp11-aout target.

Here is a simple example showing this problem:

Command: pdp11-aout-gcc -ffreestanding -nostdlib -S -o zero.s zero.c

static int zero = 0;
static int one = 1;
int main() {}

        .text
        .even

_zero:  .=.+ 02
        .data
        .even
_one:
        .word   01
        .text
        .even
        .globl  _main
_main:
        setd
        seti
        mov     r5,-(sp)
        mov     sp,r5
        jsr     pc,___main
        nop
        mov     (sp)+,r5
        rts     pc

If the first variable is initialized to a nonzero value, the output is correct:

Command: pdp11-aout-gcc -ffreestanding -nostdlib -S -o one.s one.c

static int one = 1;
static int zero = 0;
int main() {}

        .text
        .data
        .even
_one:
        .word   01
        .even

_zero:  .=.+ 02
        .text
        .even
        .globl  _main
_main:
        setd
        seti
        mov     r5,-(sp)
        mov     sp,r5
        jsr     pc,___main
        nop
        mov     (sp)+,r5
        rts     pc

My environment is a macOS host and pdp11-aout target.

GNU C17 (GCC) version 10.0.1 20200303 (experimental) (pdp11-aout)
        compiled by GNU C version 4.2.1 Compatible Apple LLVM 11.0.0
(clang-1100.0.33.17), GMP version 6.1.2, MPFR version 4.0.1, MPC version 1.1.0,
isl version isl-0.20-GMP

Configured with: ../configure --target=pdp11-aout --prefix=/usr/local
--disable-libstdc++-v3 --disable-libssp --disable-libgfortran --disable-libobjc
--disable-libbacktrace --with-gmp=/opt/local/ --with-mpfr=/opt/local/
--with-mpc=/opt/local/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug target/94134] pdp11-aout puts initial variable into .text section rather than .data
  2020-03-11  6:52 [Bug target/94134] New: pdp11-aout puts initial variable into .text section rather than .data casner at acm dot org
@ 2020-03-11  7:05 ` casner at acm dot org
  2020-03-11  7:32 ` pinskia at gcc dot gnu.org
                   ` (15 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: casner at acm dot org @ 2020-03-11  7:05 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94134

Stephen Casner <casner at acm dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Target|                            |pdp11-aout

--- Comment #1 from Stephen Casner <casner at acm dot org> ---
I would be happy to work on implementing a fix for this bug if someone can
point me in the right direction.  I tried defining a TARGET_ASM_SELECT_SECTION
function for the pdp11-aout target that always returns data_section, but the
result was unchanged.  I verified that function was only called once when
compiling my test program, that was at the point the .data directive was
emitted.

Then I stepped through the code in varasm.c to understand a bit about why this
happens.  Because the test program variable is not initialized to a nonzero
value, bss_initializer_p returns true, and consequently get_variable_section
returns lcomm_section.  Because lcomm_section is of style SECTION_NOSWITCH,
assemble_variable calls assemble_noswitch_variable to generate the output
rather than calling switch_to_section and assemble_variable_contents as is what
happens to generate the .data directive before my test program variable that
_is_ initialized to a nonzero value.

assemble_noswitch_variable generates the assembler output by calling the
callback of the section, which for lcomm_section is emit_local.  emit_local
calls ASM_OUTPUT_ALIGNED_LOCAL which has been defined for the pdp11-aout target
and implemented in pdp11_asm_output_var.  A partial fix might be to have that
function output a .bss directive ahead of the zero-initialized variable.  A
side comment is that I have not seen any .bss directive even in the output from
the real C program that led me to observe this bug.  But since we can't switch
in_section to lcomm_section, I don't know to avoid emitting .bss ahead of every
zero-initialized (or uninitialized) variable even when there are several in a
row.  Furthermore, the comment for #define SECTION_NOSWITCH says that it is for
sections that we cannot switch to with an assembler directive (like .bss), so
perhaps the logic leading to the choice of lcomm_section is not appropriate for
the pdp11_aout target.

In addition, if the "static" is removed from the zero-initialized variable
declaration in my test program so it is global, then TREE_PUBLIC is true so it
is not assigned to the lcomm_section and instead falls through to be assigned
to the .data section.  It seems that zero-initialized variables should be
assigned to the same section whether local or global.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug target/94134] pdp11-aout puts initial variable into .text section rather than .data
  2020-03-11  6:52 [Bug target/94134] New: pdp11-aout puts initial variable into .text section rather than .data casner at acm dot org
  2020-03-11  7:05 ` [Bug target/94134] " casner at acm dot org
@ 2020-03-11  7:32 ` pinskia at gcc dot gnu.org
  2020-03-11  7:34 ` pinskia at gcc dot gnu.org
                   ` (14 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: pinskia at gcc dot gnu.org @ 2020-03-11  7:32 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94134

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |coryan+gccbugzilla at google dot c
                   |                            |om

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 94085 has been marked as a duplicate of this bug. ***

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug target/94134] pdp11-aout puts initial variable into .text section rather than .data
  2020-03-11  6:52 [Bug target/94134] New: pdp11-aout puts initial variable into .text section rather than .data casner at acm dot org
  2020-03-11  7:05 ` [Bug target/94134] " casner at acm dot org
  2020-03-11  7:32 ` pinskia at gcc dot gnu.org
@ 2020-03-11  7:34 ` pinskia at gcc dot gnu.org
  2020-03-11  8:26 ` jakub at gcc dot gnu.org
                   ` (13 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: pinskia at gcc dot gnu.org @ 2020-03-11  7:34 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94134

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Most targets output .lcommon or similar for this case.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug target/94134] pdp11-aout puts initial variable into .text section rather than .data
  2020-03-11  6:52 [Bug target/94134] New: pdp11-aout puts initial variable into .text section rather than .data casner at acm dot org
                   ` (2 preceding siblings ...)
  2020-03-11  7:34 ` pinskia at gcc dot gnu.org
@ 2020-03-11  8:26 ` jakub at gcc dot gnu.org
  2020-03-11 14:48 ` jakub at gcc dot gnu.org
                   ` (12 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-03-11  8:26 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94134

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Well, in the testcase you've provided the variable isn't modified in any way
(nor used), so there is nothing wrong in optimizing it away completely or
putting it into a read-only section.
But say:
static volatile int zero = 0;
static volatile int one = 1;
int main () { zero++; one++; return 0; }
shows it is a real bug, assuming that the .text section is not modifiable on
pdp11-aout.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug target/94134] pdp11-aout puts initial variable into .text section rather than .data
  2020-03-11  6:52 [Bug target/94134] New: pdp11-aout puts initial variable into .text section rather than .data casner at acm dot org
                   ` (3 preceding siblings ...)
  2020-03-11  8:26 ` jakub at gcc dot gnu.org
@ 2020-03-11 14:48 ` jakub at gcc dot gnu.org
  2020-03-11 14:52 ` jakub at gcc dot gnu.org
                   ` (11 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-03-11 14:48 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94134

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pkoning at gcc dot gnu.org

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
The generic code wants to emit this into lcomm_section:
1203      if (ADDR_SPACE_GENERIC_P (as)
1204          && !DECL_THREAD_LOCAL_P (decl)
1205          && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1206          && bss_initializer_p (decl))
1207        {
1208          if (!TREE_PUBLIC (decl)
1209              && !((flag_sanitize & SANITIZE_ADDRESS)
1210                   && asan_protect_global (decl)))
1211            return lcomm_section;
1212          if (bss_noswitch_section)
1213            return bss_noswitch_section;
1214        }
which is a NOSWITCH section, see
https://gcc.gnu.org/ml/gcc-patches/2006-02/msg01857.html
pdp11 defines ASM_OUTPUT_ALIGNED_LOCAL to pdp11_asm_output_var (similarly for
ASM_OUTPUT_ALIGNED_COMMON), but as both are NOSWITCH sections, I think it is
wrong to emit it in whatever section is currently active.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug target/94134] pdp11-aout puts initial variable into .text section rather than .data
  2020-03-11  6:52 [Bug target/94134] New: pdp11-aout puts initial variable into .text section rather than .data casner at acm dot org
                   ` (4 preceding siblings ...)
  2020-03-11 14:48 ` jakub at gcc dot gnu.org
@ 2020-03-11 14:52 ` jakub at gcc dot gnu.org
  2020-03-11 16:57 ` pkoning at gcc dot gnu.org
                   ` (10 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-03-11 14:52 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94134

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2020-03-11
     Ever confirmed|0                           |1
           Assignee|unassigned at gcc dot gnu.org      |jakub at gcc dot gnu.org
             Status|UNCONFIRMED                 |ASSIGNED

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Created attachment 48020
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48020&action=edit
gcc10-pr94134.patch

Untested fix.  I have no way to actually test it though.
The changes in the emitted assembly on the testcase in the patch are:
@@ -1,8 +1,7 @@
         .text
+        .data
         .even
-
 _a:     .=.+ 02
-        .data
         .even
 _b:
         .word   01

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug target/94134] pdp11-aout puts initial variable into .text section rather than .data
  2020-03-11  6:52 [Bug target/94134] New: pdp11-aout puts initial variable into .text section rather than .data casner at acm dot org
                   ` (5 preceding siblings ...)
  2020-03-11 14:52 ` jakub at gcc dot gnu.org
@ 2020-03-11 16:57 ` pkoning at gcc dot gnu.org
  2020-03-11 17:37 ` jakub at gcc dot gnu.org
                   ` (9 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: pkoning at gcc dot gnu.org @ 2020-03-11 16:57 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94134

--- Comment #7 from pkoning at gcc dot gnu.org ---
Thanks Jakub.

Inspecting the generated assembly language is a sufficient check of the fix in
my view.  It's interesting that the test case shows the problem only with -O0. 
When optimizing, things are emitted in a different order (in particular, b then
a, I'm not sure why).

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug target/94134] pdp11-aout puts initial variable into .text section rather than .data
  2020-03-11  6:52 [Bug target/94134] New: pdp11-aout puts initial variable into .text section rather than .data casner at acm dot org
                   ` (6 preceding siblings ...)
  2020-03-11 16:57 ` pkoning at gcc dot gnu.org
@ 2020-03-11 17:37 ` jakub at gcc dot gnu.org
  2020-03-11 20:14 ` casner at acm dot org
                   ` (8 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-03-11 17:37 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94134

--- Comment #8 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
commit r10-7129-gd42ff1d3b62521829d90e5b972baba2a0339e2bf
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Mar 11 18:35:13 2020 +0100

    pdp11: Fix handling of common (local and global) vars [PR94134]

    As mentioned in the PR, the generic code decides to put the a variable into
    lcomm_section, which is a NOSWITCH section and thus the generic code
doesn't
    switch into a particular section before using
    ASM_OUTPUT{_ALIGNED{,_DECL}_}_LOCAL, on many targets that results just in
    .lcomm (or for non-local .comm) directives which don't need a switch to
some
    section, other targets put switch_to_section (bss_section) at the start of
    that macro.
    pdp11 doesn't do that (and doesn't have bss_section), and so emits the
    lcomm/comm variables in whatever section is current (it has only
.text/.data
    and for DEC assembler rodata).

    The following patch fixes that by putting it always into data section, and
    additionally avoids emitting an empty line in the assembly for the lcomm
    vars.

    2020-03-11  Jakub Jelinek  <jakub@redhat.com>

            PR target/94134
            * config/pdp11/pdp11.c (pdp11_asm_output_var): Call
switch_to_section
            at the start to switch to data section.  Don't print extra newline
if
            .globl directive has not been emitted.

            * gcc.c-torture/execute/pr94134.c: New test.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug target/94134] pdp11-aout puts initial variable into .text section rather than .data
  2020-03-11  6:52 [Bug target/94134] New: pdp11-aout puts initial variable into .text section rather than .data casner at acm dot org
                   ` (7 preceding siblings ...)
  2020-03-11 17:37 ` jakub at gcc dot gnu.org
@ 2020-03-11 20:14 ` casner at acm dot org
  2020-03-11 20:19 ` casner at acm dot org
                   ` (7 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: casner at acm dot org @ 2020-03-11 20:14 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94134

--- Comment #9 from Stephen Casner <casner at acm dot org> ---
Thank you all for your prompt action on this bug.  I have some comments and
questions if you are willing to help with my education about gcc internals:

1. pdp11-aout does not have a .lcommon or .lcomm section, just .text, .data and
.bss.  But also I'm missing something about the concept of a NOSWITCH_SECTION
that you can't switch to with an assembler directive -- how do you tell the
assembler to assign a variable to that section if you don't emit a directive?

2. Good point about optimization possibly omitting the variables in my test
case.  Of course, the variables were used in my real program where I first
observed this problem, and I was trying to reduce my test case to the minimum. 
If I had tried optimization and seen them removed, I would have expanded the
test case.

3. The goal that I am working toward is to have gcc and ld work correctly for
the -msplit option in pdp11-aout.  For that case, instructions and data are
stored in separate, overlapping 64KB address spaces, so even if a variable is
const it can't go in the .text section.

4. I assumed that putting switch_to_section in the ASM_OUTPUT_ALIGNED_LOCAL
function might not be legal since that is in the callback of a section so it
might confuse the state of the caller.  But if that is valid, then switching to
bss_section is the right thing to do because pdp11-aout does have .bss. 
Emitting the variable in whatever section was currently active is clearly
wrong.

5. Fixing the extra blank line for .globl is also good, but I was unsure why
the code was as it is.  Also I didn't figure out how there was a tab before
.globl since it wasn't in the string constant.

6. I agree that verifying the generated assembly language is a sufficient
check.  But the way I run this code now is in emulation using SimH.

7. Emitting the zero-initialized variable into .data when it happens to follow
a nonzero-initialized variable is also not correct, or at least suboptimal.  If
there is a vary large uninitialized array, that would make the final executable
output file much larger.  (OK, these days a full 64KB is still a pittance, but
on principal it's suboptimal.)  Therefore, switching to .bss in
ASM_OUTPUT{_ALIGNED{,_DECL}_}_LOCAL is good.

8. When the variables are not static (hence global), then TREE_PUBLIC is true
so it is not assigned to the lcomm_section and instead falls through to be
assigned to the .data section.  It seems that zero-initialized variables should
be assigned to the same section whether local or global.  So I think we do need
a TARGET_ASM_SELECT_SECTION function for the pdp11-aout target implemented by a
pdp11_select_section function that looks at bss_initializer_p (decl) to choose
between .data and .bss.  Or does it need anything more of the logic like this
from get_variable_section, or maybe define bss_noswitch_section to be
bss_section if that makes sense?

  if (ADDR_SPACE_GENERIC_P (as)
      && !DECL_THREAD_LOCAL_P (decl)
      && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
      && bss_initializer_p (decl))
    {
      if (!TREE_PUBLIC (decl)
          && !((flag_sanitize & SANITIZE_ADDRESS)
               && asan_protect_global (decl)))
        return lcomm_section;
      if (bss_noswitch_section)
        return bss_noswitch_section;
    }

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug target/94134] pdp11-aout puts initial variable into .text section rather than .data
  2020-03-11  6:52 [Bug target/94134] New: pdp11-aout puts initial variable into .text section rather than .data casner at acm dot org
                   ` (8 preceding siblings ...)
  2020-03-11 20:14 ` casner at acm dot org
@ 2020-03-11 20:19 ` casner at acm dot org
  2020-03-11 21:10 ` pkoning at gcc dot gnu.org
                   ` (6 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: casner at acm dot org @ 2020-03-11 20:19 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94134

--- Comment #10 from Stephen Casner <casner at acm dot org> ---
One more question:  I'm working on binutils ld as well, wanting to implement a
new option --imagic for pdp11-aout that outputs magic number 0413 and sets both
.text and .data at address 0 (with .bss to follow .data).  If -msplit is
included as a compilation option, then --imagic should be passed to the linker.
 I'd appreciate a pointer to where that should happen.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug target/94134] pdp11-aout puts initial variable into .text section rather than .data
  2020-03-11  6:52 [Bug target/94134] New: pdp11-aout puts initial variable into .text section rather than .data casner at acm dot org
                   ` (9 preceding siblings ...)
  2020-03-11 20:19 ` casner at acm dot org
@ 2020-03-11 21:10 ` pkoning at gcc dot gnu.org
  2020-03-11 23:18 ` casner at acm dot org
                   ` (5 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: pkoning at gcc dot gnu.org @ 2020-03-11 21:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94134

--- Comment #11 from pkoning at gcc dot gnu.org ---
(In reply to Stephen Casner from comment #9)

A lot of these questions should have answers in the GCC Internals manual, which
is quite good.  And also quite dense.  I know it a little, enough for a bunch
of cleanup on the pdp11 target in recent years, but I'm certainly not an
expert.  The gcc mailing list is where you find experts.  Some comments below
for the parts I can answer:

> ...
> 2. Good point about optimization possibly omitting the variables in my test
> case.  Of course, the variables were used in my real program where I first
> observed this problem, and I was trying to reduce my test case to the
> minimum.  If I had tried optimization and seen them removed, I would have
> expanded the test case.

The test case was fine.  What I meant is that when I compiled with -O1, the
back end would emit b followed by a, which wouldn't show the problem because
emitting b would correctly send the .data even in the old code.
> 
> 3. The goal that I am working toward is to have gcc and ld work correctly
> for the -msplit option in pdp11-aout.  For that case, instructions and data
> are stored in separate, overlapping 64KB address spaces, so even if a
> variable is const it can't go in the .text section.

Yes.  Ideally as/ld would support .rodata, but without that it has to be .data. 

>... 
> 5. Fixing the extra blank line for .globl is also good, but I was unsure why
> the code was as it is.  Also I didn't figure out how there was a tab before
> .globl since it wasn't in the string constant.

The assembler doesn't actually care about any of that, so it's just a matter of
aiming for reasonable consistency to make the assembly code somewhat readable. 
The code generator isn't all that consistent, but there isn't any reason for
that as far as I am aware.

>...
> 7. Emitting the zero-initialized variable into .data when it happens to
> follow a nonzero-initialized variable is also not correct, or at least
> suboptimal.  If there is a vary large uninitialized array, that would make
> the final executable output file much larger.  (OK, these days a full 64KB
> is still a pittance, but on principal it's suboptimal.)  Therefore,
> switching to .bss in ASM_OUTPUT{_ALIGNED{,_DECL}_}_LOCAL is good.

Yes, if the assembler/linker supports that, but my understanding is that they
do not.  It would be lovely if we could have ELF for pdp11...

---
On the linker option tied to -msplit, I haven't used this but the GCC Internals
manual gives the answer, in "18.2 Controlling the Compilation Driver, ‘gcc’" --
LINK_SPEC will do this.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug target/94134] pdp11-aout puts initial variable into .text section rather than .data
  2020-03-11  6:52 [Bug target/94134] New: pdp11-aout puts initial variable into .text section rather than .data casner at acm dot org
                   ` (10 preceding siblings ...)
  2020-03-11 21:10 ` pkoning at gcc dot gnu.org
@ 2020-03-11 23:18 ` casner at acm dot org
  2020-03-11 23:43 ` casner at acm dot org
                   ` (4 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: casner at acm dot org @ 2020-03-11 23:18 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94134

--- Comment #12 from Stephen Casner <casner at acm dot org> ---
(In reply to pkoning from comment #11)
> (In reply to Stephen Casner from comment #9)
> > 7. Emitting the zero-initialized variable into .data when it happens to
> > follow a nonzero-initialized variable is also not correct, or at least
> > suboptimal.  If there is a vary large uninitialized array, that would make
> > the final executable output file much larger.  (OK, these days a full 64KB
> > is still a pittance, but on principal it's suboptimal.)  Therefore,
> > switching to .bss in ASM_OUTPUT{_ALIGNED{,_DECL}_}_LOCAL is good.
> 
> Yes, if the assembler/linker supports that, but my understanding is that
> they do not.  It would be lovely if we could have ELF for pdp11...

Although .bss is not defined as a general GNU Assembler directive, it is
defined as a PDP-11 dependent feature.  It was also included in the PDP11 Unix
assembly language as produced by cc on 2.11BSD, for example:

user[47] cat > array.c
static int zero[1000];
static int one[1000] = { 1 };
int main() {}
user[48] cc -S -o array.s array.c
user[49] cat array.s
.globl  
.bss
_zero:
.=.+3720
.data
_one:
1
.=.+3716
.globl  _main
.text
_main:
~~main:
jsr     r5,csv
jbr     L1
L2:L3:jmp       cret
L1:jbr  L2
.globl
.data
user[50] 

This array.s is not accepted by GNU as because of the .globl without a symbol,
the constant 1 without .word, and the jbr mnemonic.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug target/94134] pdp11-aout puts initial variable into .text section rather than .data
  2020-03-11  6:52 [Bug target/94134] New: pdp11-aout puts initial variable into .text section rather than .data casner at acm dot org
                   ` (11 preceding siblings ...)
  2020-03-11 23:18 ` casner at acm dot org
@ 2020-03-11 23:43 ` casner at acm dot org
  2020-03-17 18:57 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: casner at acm dot org @ 2020-03-11 23:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94134

--- Comment #13 from Stephen Casner <casner at acm dot org> ---
(In reply to Stephen Casner from comment #9)
(Commenting on my own comment)
> 1. pdp11-aout does not have a .lcommon or .lcomm section, just .text, .data
> and .bss.  But also I'm missing something about the concept of a
> NOSWITCH_SECTION that you can't switch to with an assembler directive -- how
> do you tell the assembler to assign a variable to that section if you don't
> emit a directive?

The a.out file format produced by the assembler or linker includes only the
.text, .data and .bss sections.  However, that does not mean the compiler could
not pass a .comm or .lcomm directive to the assembler.  I now understand those
and the NOSWITCH concept.  In fact, I found that the Unix v6 cc produces the
.comm directive which I had not seen before:

# cat > static.c
int zero;
int one;
int main() {
    static int two;
    zero = 0;
    one = 1;
    two = 2;
}
# cc -S static.s static.c
# cat static.s
.globl  _zero
.comm   _zero,2
.globl  _one
.comm   _one,2
.globl  _main
.text
_main:
~~main:
.bss
L2:.=.+2
.text
~two=L2
jsr     r5,csv
clr     _zero
mov     $1,_one
mov     $2,L2
L1:jmp  cret
.globl
.data
# 

So presumably the ASM_OUTPUT{_ALIGNED{,_DECL}_}_LOCAL functions could generate
.comm and .lcomm directives to be allocated to the .bss section by as.  I don't
know if there is enough size information in the relocation section of the a.out
file to allow merging global .comm declarations.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug target/94134] pdp11-aout puts initial variable into .text section rather than .data
  2020-03-11  6:52 [Bug target/94134] New: pdp11-aout puts initial variable into .text section rather than .data casner at acm dot org
                   ` (12 preceding siblings ...)
  2020-03-11 23:43 ` casner at acm dot org
@ 2020-03-17 18:57 ` cvs-commit at gcc dot gnu.org
  2020-09-17 14:24 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-03-17 18:57 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94134

--- Comment #14 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-9 branch has been updated by Jakub Jelinek
<jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:f1125cf88ac0c97d819e4f81d556fbcd1161270e

commit r9-8391-gf1125cf88ac0c97d819e4f81d556fbcd1161270e
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Mar 11 18:35:13 2020 +0100

    pdp11: Fix handling of common (local and global) vars [PR94134]

    As mentioned in the PR, the generic code decides to put the a variable into
    lcomm_section, which is a NOSWITCH section and thus the generic code
doesn't
    switch into a particular section before using
    ASM_OUTPUT{_ALIGNED{,_DECL}_}_LOCAL, on many targets that results just in
    .lcomm (or for non-local .comm) directives which don't need a switch to
some
    section, other targets put switch_to_section (bss_section) at the start of
    that macro.
    pdp11 doesn't do that (and doesn't have bss_section), and so emits the
    lcomm/comm variables in whatever section is current (it has only
.text/.data
    and for DEC assembler rodata).

    The following patch fixes that by putting it always into data section, and
    additionally avoids emitting an empty line in the assembly for the lcomm
    vars.

    2020-03-11  Jakub Jelinek  <jakub@redhat.com>

            PR target/94134
            * config/pdp11/pdp11.c (pdp11_asm_output_var): Call
switch_to_section
            at the start to switch to data section.  Don't print extra newline
if
            .globl directive has not been emitted.

            * gcc.c-torture/execute/pr94134.c: New test.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug target/94134] pdp11-aout puts initial variable into .text section rather than .data
  2020-03-11  6:52 [Bug target/94134] New: pdp11-aout puts initial variable into .text section rather than .data casner at acm dot org
                   ` (13 preceding siblings ...)
  2020-03-17 18:57 ` cvs-commit at gcc dot gnu.org
@ 2020-09-17 14:24 ` cvs-commit at gcc dot gnu.org
  2020-09-17 17:18 ` jakub at gcc dot gnu.org
  2020-09-18  5:27 ` casner at acm dot org
  16 siblings, 0 replies; 18+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-09-17 14:24 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94134

--- Comment #15 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-8 branch has been updated by Jakub Jelinek
<jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:4910b2e4cfe25c95fef18cf54125b788c190cfb2

commit r8-10462-g4910b2e4cfe25c95fef18cf54125b788c190cfb2
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Mar 11 18:35:13 2020 +0100

    pdp11: Fix handling of common (local and global) vars [PR94134]

    As mentioned in the PR, the generic code decides to put the a variable into
    lcomm_section, which is a NOSWITCH section and thus the generic code
doesn't
    switch into a particular section before using
    ASM_OUTPUT{_ALIGNED{,_DECL}_}_LOCAL, on many targets that results just in
    .lcomm (or for non-local .comm) directives which don't need a switch to
some
    section, other targets put switch_to_section (bss_section) at the start of
    that macro.
    pdp11 doesn't do that (and doesn't have bss_section), and so emits the
    lcomm/comm variables in whatever section is current (it has only
.text/.data
    and for DEC assembler rodata).

    The following patch fixes that by putting it always into data section, and
    additionally avoids emitting an empty line in the assembly for the lcomm
    vars.

    2020-03-11  Jakub Jelinek  <jakub@redhat.com>

            PR target/94134
            * config/pdp11/pdp11.c (pdp11_asm_output_var): Call
switch_to_section
            at the start to switch to data section.  Don't print extra newline
if
            .globl directive has not been emitted.

            * gcc.c-torture/execute/pr94134.c: New test.

    (cherry picked from commit f1125cf88ac0c97d819e4f81d556fbcd1161270e)

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug target/94134] pdp11-aout puts initial variable into .text section rather than .data
  2020-03-11  6:52 [Bug target/94134] New: pdp11-aout puts initial variable into .text section rather than .data casner at acm dot org
                   ` (14 preceding siblings ...)
  2020-09-17 14:24 ` cvs-commit at gcc dot gnu.org
@ 2020-09-17 17:18 ` jakub at gcc dot gnu.org
  2020-09-18  5:27 ` casner at acm dot org
  16 siblings, 0 replies; 18+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-09-17 17:18 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94134

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #16 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Should be now fixed for 8.5 and 9.4+ too.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug target/94134] pdp11-aout puts initial variable into .text section rather than .data
  2020-03-11  6:52 [Bug target/94134] New: pdp11-aout puts initial variable into .text section rather than .data casner at acm dot org
                   ` (15 preceding siblings ...)
  2020-09-17 17:18 ` jakub at gcc dot gnu.org
@ 2020-09-18  5:27 ` casner at acm dot org
  16 siblings, 0 replies; 18+ messages in thread
From: casner at acm dot org @ 2020-09-18  5:27 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94134

--- Comment #17 from Stephen Casner <casner at acm dot org> ---
Thanks.

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2020-09-18  5:27 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-11  6:52 [Bug target/94134] New: pdp11-aout puts initial variable into .text section rather than .data casner at acm dot org
2020-03-11  7:05 ` [Bug target/94134] " casner at acm dot org
2020-03-11  7:32 ` pinskia at gcc dot gnu.org
2020-03-11  7:34 ` pinskia at gcc dot gnu.org
2020-03-11  8:26 ` jakub at gcc dot gnu.org
2020-03-11 14:48 ` jakub at gcc dot gnu.org
2020-03-11 14:52 ` jakub at gcc dot gnu.org
2020-03-11 16:57 ` pkoning at gcc dot gnu.org
2020-03-11 17:37 ` jakub at gcc dot gnu.org
2020-03-11 20:14 ` casner at acm dot org
2020-03-11 20:19 ` casner at acm dot org
2020-03-11 21:10 ` pkoning at gcc dot gnu.org
2020-03-11 23:18 ` casner at acm dot org
2020-03-11 23:43 ` casner at acm dot org
2020-03-17 18:57 ` cvs-commit at gcc dot gnu.org
2020-09-17 14:24 ` cvs-commit at gcc dot gnu.org
2020-09-17 17:18 ` jakub at gcc dot gnu.org
2020-09-18  5:27 ` casner at acm dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).