mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* Re: Writable global section?
       [not found] <Pine.LNX.3.95.1021009090100.31255A-100000@chaos.analogic.com>
@ 2002-10-09 13:08 ` Richard B. Johnson
  2002-10-09 14:24   ` J.A. Magallon
  0 siblings, 1 reply; 9+ messages in thread
From: Richard B. Johnson @ 2002-10-09 13:08 UTC (permalink / raw)
  To: Linux kernel


 
 When using shared libraries, is there a ".section" into which
 I can put a variable that's writable? I note that when programs
 that use shared libraries start, the pages are mprotect(ed)
 PROT_READ|PROT_EXEC, but sometimes I see PROT_WRITE on some
 pages.
 
 I'd like to rip out a memory-mapped semiphore and put it directly
 in a shared library if possible.
 

Cheers,
Dick Johnson
Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips).
The US military has given us many words, FUBAR, SNAFU, now ENRON.
Yes, top management were graduates of West Point and Annapolis.


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

* Re: Writable global section?
  2002-10-09 13:08 ` Writable global section? Richard B. Johnson
@ 2002-10-09 14:24   ` J.A. Magallon
  2002-10-09 14:49     ` Richard B. Johnson
  0 siblings, 1 reply; 9+ messages in thread
From: J.A. Magallon @ 2002-10-09 14:24 UTC (permalink / raw)
  To: root; +Cc: Linux kernel


On 2002.10.09 Richard B. Johnson wrote:
>
> 
> When using shared libraries, is there a ".section" into which
> I can put a variable that's writable? I note that when programs
> that use shared libraries start, the pages are mprotect(ed)
> PROT_READ|PROT_EXEC, but sometimes I see PROT_WRITE on some
> pages.
> 
> I'd like to rip out a memory-mapped semiphore and put it directly
> in a shared library if possible.
> 

A library can define global variables visible to others, has its own BSS:

int x;

void f()
{
}

built with gcc -shared, and nm'ed gives:

00000694 T f
000017f8 B x
000016f8 D y

from man nm:
  "B" The symbol is in the uninitialized data section (known as BSS).
  "D" The symbol is in the initialized data section.
  "T" The symbol is in the text (code) section.

Was about this or I misunderstood you ?

-- 
J.A. Magallon <jamagallon@able.es>      \                 Software is like sex:
werewolf.able.es                         \           It's better when it's free
Mandrake Linux release 9.1 (Cooker) for i586
Linux 2.4.20-pre10-jam1 (gcc 3.2 (Mandrake Linux 9.0 3.2-2mdk))

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

* Re: Writable global section?
  2002-10-09 14:24   ` J.A. Magallon
@ 2002-10-09 14:49     ` Richard B. Johnson
  2002-10-09 15:06       ` Brian Gerst
                         ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Richard B. Johnson @ 2002-10-09 14:49 UTC (permalink / raw)
  To: J.A. Magallon; +Cc: Linux kernel

On Wed, 9 Oct 2002, J.A. Magallon wrote:

> 
> On 2002.10.09 Richard B. Johnson wrote:
> >
> > 
> > When using shared libraries, is there a ".section" into which
> > I can put a variable that's writable? I note that when programs
> > that use shared libraries start, the pages are mprotect(ed)
> > PROT_READ|PROT_EXEC, but sometimes I see PROT_WRITE on some
> > pages.
> > 
> > I'd like to rip out a memory-mapped semiphore and put it directly
> > in a shared library if possible.
> > 
> 
> A library can define global variables visible to others, has its own BSS:
> 
> int x;
> 
> void f()
> {
> }
> 
> built with gcc -shared, and nm'ed gives:
> 
> 00000694 T f
> 000017f8 B x
> 000016f8 D y
> 
> from man nm:
>   "B" The symbol is in the uninitialized data section (known as BSS).
>   "D" The symbol is in the initialized data section.
>   "T" The symbol is in the text (code) section.
> 
> Was about this or I misunderstood you ?

No. You did not misunderstand. If I declare a variable and put it
into a shared library, I seem to get one of two different behaviors.

If the variable is initialized, it goes into the ".data" section.
If the variable is not initialized, it goes into the ".bss" section.
Non-writable strings go into ".rodata"

In the case of data in the .bss section, if one procedure writes to
this variable, it is not seen by other procedures that are linked
to the shared library. However it can write with no problem and it
can read what it wrote. Apparently ".bss" data are not really allocations
in shared memory, only a promise to allocate some data when the program
is loaded and this data is not shared, it's private to the process.

If a variable is in the ".data" section, it is "seen" by all procedures
that are linked to the shared library, but any attempt to write to this
variable will seg-fault the task that attempts to modify it.

I would like to be able to write to that variable and have it seen
by other tasks, since shared memory is shared memory. It's a shame
to mmap a shared library upon startup and then have to mmap some
additional shared memory for some inter-process communication.

So, is there a "section" that, when mmapped by the loader will
not protect it from writes?

Cheers,
Dick Johnson
Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips).
The US military has given us many words, FUBAR, SNAFU, now ENRON.
Yes, top management were graduates of West Point and Annapolis.


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

* Re: Writable global section?
  2002-10-09 14:49     ` Richard B. Johnson
@ 2002-10-09 15:06       ` Brian Gerst
  2002-10-09 15:46         ` Richard B. Johnson
  2002-10-09 15:06       ` Andreas Schwab
  2002-10-09 23:14       ` Jamie Lokier
  2 siblings, 1 reply; 9+ messages in thread
From: Brian Gerst @ 2002-10-09 15:06 UTC (permalink / raw)
  To: root; +Cc: J.A. Magallon, Linux kernel

Richard B. Johnson wrote:
> I would like to be able to write to that variable and have it seen
> by other tasks, since shared memory is shared memory. It's a shame
> to mmap a shared library upon startup and then have to mmap some
> additional shared memory for some inter-process communication.

There are only two ways to share memory between processes:
- SYSV shared memory
- using clone() to share the VM.

Shared libraries != shared memory.  Each mapping of a shared library is 
copy-on-write.  The purpose of shared libraries is to save memory, not 
for IPC.

--
				Brian Gerst



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

* Re: Writable global section?
  2002-10-09 14:49     ` Richard B. Johnson
  2002-10-09 15:06       ` Brian Gerst
@ 2002-10-09 15:06       ` Andreas Schwab
  2002-10-09 15:56         ` Richard B. Johnson
  2002-10-09 23:14       ` Jamie Lokier
  2 siblings, 1 reply; 9+ messages in thread
From: Andreas Schwab @ 2002-10-09 15:06 UTC (permalink / raw)
  To: root; +Cc: J.A. Magallon, Linux kernel

"Richard B. Johnson" <root@chaos.analogic.com> writes:

|> If a variable is in the ".data" section, it is "seen" by all procedures
|> that are linked to the shared library, but any attempt to write to this
|> variable will seg-fault the task that attempts to modify it.

Your tests must be flawed, because a .data section *is* writable.  The
only difference between .data and .bss is that the latter has no
allocation in the image file, but they are mapped to the same, writable
segment.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux AG, Deutschherrnstr. 15-19, D-90429 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

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

* Re: Writable global section?
  2002-10-09 15:06       ` Brian Gerst
@ 2002-10-09 15:46         ` Richard B. Johnson
  0 siblings, 0 replies; 9+ messages in thread
From: Richard B. Johnson @ 2002-10-09 15:46 UTC (permalink / raw)
  To: Brian Gerst; +Cc: J.A. Magallon, Linux kernel

On Wed, 9 Oct 2002, Brian Gerst wrote:

> Richard B. Johnson wrote:
> > I would like to be able to write to that variable and have it seen
> > by other tasks, since shared memory is shared memory. It's a shame
> > to mmap a shared library upon startup and then have to mmap some
> > additional shared memory for some inter-process communication.
> 
> There are only two ways to share memory between processes:
> - SYSV shared memory
> - using clone() to share the VM.
> 
> Shared libraries != shared memory.  Each mapping of a shared library is 
> copy-on-write.  The purpose of shared libraries is to save memory, not 
> for IPC.
> 
> --
> 				Brian Gerst

It's a shame we don't have writable global sections in shared libraries
like the old VAXen did.

Cheers,
Dick Johnson
Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips).
The US military has given us many words, FUBAR, SNAFU, now ENRON.
Yes, top management were graduates of West Point and Annapolis.


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

* Re: Writable global section?
  2002-10-09 15:06       ` Andreas Schwab
@ 2002-10-09 15:56         ` Richard B. Johnson
  2002-10-09 16:01           ` Andreas Schwab
  0 siblings, 1 reply; 9+ messages in thread
From: Richard B. Johnson @ 2002-10-09 15:56 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: J.A. Magallon, Linux kernel

On Wed, 9 Oct 2002, Andreas Schwab wrote:

> "Richard B. Johnson" <root@chaos.analogic.com> writes:
> 
> |> If a variable is in the ".data" section, it is "seen" by all procedures
> |> that are linked to the shared library, but any attempt to write to this
> |> variable will seg-fault the task that attempts to modify it.
> 
> Your tests must be flawed, because a .data section *is* writable.  The
> only difference between .data and .bss is that the latter has no
> allocation in the image file, but they are mapped to the same, writable
> segment.
> 
> Andreas.

Well, yes I found out.. This anomaly with the assembler.....

.section .data
.global	pars
.type	pars,@object
.size	pars,4
.align  4
pars:	.long	0
.end


I accidentally left out .size, guess what? Even though I had an
offset recognized and a ".long", initialized to 0, there was no
space allocated and therefore the seg-fault. I would have seen
this, but the problem doesn't exist if the ".section" is ".bss",
the first section I was messing with. Go figure?

And the writable, is a COW. It isn't seen by others. It's a shame.
It would be very useful to have a writable global section available 
like VAXen did.


Cheers,
Dick Johnson
Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips).
The US military has given us many words, FUBAR, SNAFU, now ENRON.
Yes, top management were graduates of West Point and Annapolis.


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

* Re: Writable global section?
  2002-10-09 15:56         ` Richard B. Johnson
@ 2002-10-09 16:01           ` Andreas Schwab
  0 siblings, 0 replies; 9+ messages in thread
From: Andreas Schwab @ 2002-10-09 16:01 UTC (permalink / raw)
  To: root; +Cc: J.A. Magallon, Linux kernel

"Richard B. Johnson" <root@chaos.analogic.com> writes:

|> Well, yes I found out.. This anomaly with the assembler.....
|> 
|> .section .data
|> .global	pars
|> .type	pars,@object
|> .size	pars,4
|> .align  4
|> pars:	.long	0
|> .end
|> 
|> 
|> I accidentally left out .size, guess what? Even though I had an
|> offset recognized and a ".long", initialized to 0, there was no
|> space allocated and therefore the seg-fault. I would have seen
|> this, but the problem doesn't exist if the ".section" is ".bss",
|> the first section I was messing with. Go figure?

I think this problem only exists on platforms with COPY relocations.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux AG, Deutschherrnstr. 15-19, D-90429 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

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

* Re: Writable global section?
  2002-10-09 14:49     ` Richard B. Johnson
  2002-10-09 15:06       ` Brian Gerst
  2002-10-09 15:06       ` Andreas Schwab
@ 2002-10-09 23:14       ` Jamie Lokier
  2 siblings, 0 replies; 9+ messages in thread
From: Jamie Lokier @ 2002-10-09 23:14 UTC (permalink / raw)
  To: Richard B. Johnson; +Cc: J.A. Magallon, Linux kernel

Richard B. Johnson wrote:
> In the case of data in the .bss section, if one procedure writes to
> this variable, it is not seen by other procedures that are linked
> to the shared library. However it can write with no problem and it
> can read what it wrote. Apparently ".bss" data are not really allocations
> in shared memory, only a promise to allocate some data when the program
> is loaded and this data is not shared, it's private to the process.

You are mixing up two very different concepts.

Writes to both .bss and .data allocations are seen by _all_ procedures
that are linked together.  Run-time linking is within a single
processes only.  So writes to any section are private to the process
which does the writes.

.bss and .data are nearly the same thing: writable, process-private
areas.  The only difference is that .data is initialised from the
shared library file, while .bss is initialised with zeros.

Don't think about "mapping" a shared library, because the mappings
aren't like shared memory between processes.  That's misleading if you
come from a VAX or Windows background, where they are.

Think about "loading" a shared library instead: as if you'd allocated
private memory in a process and then copied the library file into that
memory.  You could rewrite the ELF loader to use malloc+read+mprotect,
and every program should continue to work.

(Ignore the fact that mmap() is used: it's an optimisation which
doesn't change the behaviour of the program).

> If a variable is in the ".data" section, it is "seen" by all procedures
> that are linked to the shared library, but any attempt to write to this
> variable will seg-fault the task that attempts to modify it.

No it won't.  (Unless you managed to declare the ".data" section read
only, which is not possible normally, but may be possible with certain
tiny assembly language test programs).

> I would like to be able to write to that variable and have it seen
> by other tasks, since shared memory is shared memory.

Note that, in unix terminology, the phrase "linked to a shared
library" means linkage within a single private process only, and
the term "shared library" has very little to do with "shared memory".

This is not like Windows, where there is effectively one instance of
the shared library mapped and shared between processes, and the
library knows about the multiple processes that are using it.

In Linux each process creates its _own_ instance of the library at
load time, and each of those instances is private to the process that
created it.

> It's a shame to mmap a shared library upon startup and then have to
> mmap some additional shared memory for some inter-process
> communication.

Perhaps but it does force you to think about what extent of sharing
you really want, instead of giving you the one option which is often
wrong.

There are occasions when you'd want multiple processes per user to
share some data, but different users to _not_ share anything.  There
are other occasions when you want different users to share data.
Sometimes you'd really like the data shared within a cluster instead
of on a single machine.  Sometimes you'd like the data shared per X
server, for example a web browser using a process per browser window
might need this.

You said you wanted the shared area for a semaphore.  Ok, but what is
the semaphore protecting?  If it's access to a file such as a
database, the semaphore should be _network_ wide because files are not
always local.

If it's protecting access to a set of per-user files, map a lock file
in each user's home directoy.  If it's a scoreboard for a host-wide
service such as a web server, it wants to use a host-wide file such as
in /var/run for example.

In general, a semaphore should have similar scope to the thing it's
protecting.  So, a file lock for a file or set of related files; a
thread semaphore to protect data in a thread from other threads; an
inter-process semaphore using explicit IPC if you have an explicitly
shared segment; a network daemon to synchronise access to a network
wide resource, etc.

Btw, take a look at pthreads especially the latest Glibc pthreads
thing Ulrich & Ingo have worked on.  It offers fast & precise
inter-thread (process scope) and inter-process semaphores, based on
futexes, I believe.

-- Jamie

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

end of thread, other threads:[~2002-10-09 23:08 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <Pine.LNX.3.95.1021009090100.31255A-100000@chaos.analogic.com>
2002-10-09 13:08 ` Writable global section? Richard B. Johnson
2002-10-09 14:24   ` J.A. Magallon
2002-10-09 14:49     ` Richard B. Johnson
2002-10-09 15:06       ` Brian Gerst
2002-10-09 15:46         ` Richard B. Johnson
2002-10-09 15:06       ` Andreas Schwab
2002-10-09 15:56         ` Richard B. Johnson
2002-10-09 16:01           ` Andreas Schwab
2002-10-09 23:14       ` Jamie Lokier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®