...making Linux just a little more fun!

<-- prev | next -->

# More 2 Cent Tips!

See also: The Answer Gang's Knowledge Base and the LG Search Engine

- a question about linker script
- Launching Firefox 0.9
- Checkinstall
- Periodic table of the operators
- "Remote" authentication with PHP
- Xserver spits Xclient hosts
- Fun game in 64 LoC
- Grokdoc

### a question about linker script

Ben Okopnik, Jimmy O'Regan, Philip O Brien (The LG Answer Gang)
Question by Liang Jun-Ming (helo54ljm from yahoo.com.tw)

Dear Mr.Okopnik

My name is Liang Jun-Ming,a student from Taiwan.

I'm suffering from a programming problem,I would very much appreciate receving an answer from you ! I wrote a testing program named hello.s , and a linker script named ldscript as follows:

See attached `liong.hello.s.asm.txt`

after " as hello.s -o hello.o" and "ld -Tldscript hello.o -o hello" I get an executable named hello.

My question is:

> What does "AT(0x3000000)" mean ?

Does it mean that the loader will load this executable at "physical address " 0x3000000 ?

I know the "virtual address" of the first byte of this executable would be 0x1000, but in Linux , can we put data at any physical address as we wish ? If it does, why the program still works even if the RAM is 32M in my PC ?(0x3000000 > 32M). What tool in Linux can we have to let us "watch" the contents of memory by specifying "physical addresses"? If it doesn't, what is the function when we write "AT(xxxx)" ? Hope I can receive an answer when you are not busy. Thank you very much!

> [Ben] Hi, Jun-Ming -

> I'm afraid your question is somewhat misdirected: I'm not an assembly expert, under Linux or otherwise. My suggestion to you would be to start at http://linuxassembly.org and go from there - their front page reads

............... Welcome to the Linux Assembly! If you are looking for information on assembly programming under UNIX-like operating systems (Linux/BSD/ BeOS/etc), this is the right place to be. Here you can find various resources, ranging from tutorials and documentation, to actual programs written in assembly language. As time passes, we will try to provide as much information on the subject as possible, so stay tuned. ...............

> That seems like the right place for your query.

> [Jimmy O'Regan] The `AT()` keyword is for things like ROMs. I recommend you read the GNU Linker manual:

> > http://www.gnu.org/software/binutils/manual/ld-2.9.1/html_chapter/ld_3.html#SEC21

Dear Answer Gang:

> According to the GNU linker ld manual:

the `AT()` keywod specifies the load address of a section, and it says "this feature is designed to make it easy to build a ROM image..."

Query,

1. the address specified by AT() keyword to load is the "physical address" of ROM ? and we can really put code in ROM in this way ?
2. the "ROM" the document mentioned is the actual element in board, or just RAM of imitating the Read-Only-Memory ?

( forgive me for a Chinese student's poor English !)

> [Jimmy] The `AT()` keyword can be used for ROMs - if you want to write a replacement BIOS (such as Linux BIOS: http://www.linuxbios.org), you will have to do use the `AT()` keyword; or if you are creating a ROM images for later use in an emulator, or to write to a physical ROM.

> You can also use the `AT()` keyword for boot loaders/kernels, which need to be at a specific location in RAM to be executed; and in a normal program. If the program has a specified load address, and is loaded on an MMU-less system, or in an OS which doesn't use the MMU, then you get access to that specific area of memory, but in an OS (such as Linux) which makes use of the MMU, the address is transparently remapped to an address within the space allocated to your program by the kernel.

> Jimmy also asked a friend... -- Heather

> [Jimmy] Sorry about the format this takes; I asked a friend from college if he could answer the linker script question, the answer is in the attached IRC log. There's a lot of overview in there which I thought was interesting. Philip's credentials are that he wrote an operating system for his degree thesis, based on KeyOS (http://keyos.sourceforge.net), to which he contributed code under a pseudonym (the college made some claims about copyright...).

> Logged from the UnderNet IRC on Jun 20.
> -- Heather

............... `<JimRegan> So, can you answer that question? <JimRegan> Cos noone at LG can.` <Crypty> question? `<JimRegan> In the html file I sent` <Crypty> its the offset that that section is loaded at. <Crypty> the offset from the loadaddr `<JimRegan> ?` <Crypty> and the 0x1000 is the size of the section if i remember correctly <Crypty> or at least i think thats it. <Crypty> had to figure it out for writing my boot loader, which i couldnt get to work right, caused fatal exception when it switched to ring 0 to enter PMODE..... <Crypty> ended up having to use Grub, but handed up my effort to the examiners, they were impressed i got it to ge tthat faer. `<JimRegan> So... in a ROM, it'd go from the beginning of memory, otherwise it's relative to where it loaded <JimRegan> If the ROM is loaded at 0` <Crypty> .text is actually the code by the way, not the strings. <Crypty> and yes, but all programs see their address as 0, but the OS handles their requests for memory locations and adds their offset in the memory manager `<JimRegan> The strings go in .data` <Crypty> yes <Crypty> the code is put in a large offset to avoid overflows causing mistooks in the code. `<JimRegan> So it's not necessary to specify the location, unless you have some weird backwards compatibility problem, or are writing an OS?` <Crypty> but the OS can preempt this and actually fool the program by pretending it loaded the code at that offset and translating the addresses on the fly <Crypty> well, it is also to specify locations in writing an OS or other "pure application" `<JimRegan> So, in short, you can't just put code wherever the heck you want in a modern OS?` <Crypty> but a decent OS will just ignore the offsets and pretend instead <Crypty> no, the OS itself has to obey the offset rules, as the memory manager will not be loaded at that point. `<JimRegan> I mean if that's executed in a running OS.` <Crypty> so it cant pretent and every thing is relying on exact offsets for a form of crude task switching. <Crypty> oh, yes. `<JimRegan> Like the MacOS did?` <Crypty> but even without a ld script, it should still use a default set of offsets for linking, depending on ythe executable format, coff, elf, bin etc.. `<JimRegan> OK, I think that'll answer the question. Do I have your formal permission to send this for potential publication?` <Crypty> the offsets are set by default on set types of exe, so that the os can just go , heh I know this type, and not have to search for markers in the code etc. <Crypty> no, hehe I better read up and make sure its right first.. its been over a year. `<JimRegan> Ah. Cool. <JimRegan> Well, what you've said makes what I read in the LD manual make sense.` <Crypty> oh well in that cvase.. sure.. just dont sign my name to it, klast thing i need is people with questions :-) or worse some git flaming me for not getting it quite right. `<JimRegan> AT ( ldadr ) <JimRegan> The expression ldadr that follows the AT keyword specifies the load address of the section. The default (if you do not use the AT keyword) is to make the load address the same as the relocation address. This feature is designed to make it easy to build a ROM image. For example, this SECTIONS definition creates two output sections: one called `.text', which starts at 0x1000, and one called `.mdata', w <JimRegan> hich is loaded at the end of the `.text' section even though its relocation address is 0x2000. The symbol _data is defined with the value 0x2000:` <Crypty> but that said I think i got it right, been a while but I think i remember it right <Crypty> oh, good I did remember it right.. yeah, go me! `<JimRegan> So, do I get a definite OK now?` <Crypty> hehe I am still one of the knigits of ye olde ASM coding :-) <Crypty> yeah sure. `<JimRegan> Thanks.` <Crypty> you can tell me if some one flames you for getting it slightly skew ways, <Crypty> but it is a good enough explainatin to the question i think `<JimRegan> Nah. People are generally polite in pointing out mistakes. <JimRegan> God knows I've made enough of them.` <Crypty> AT() is definitly the offset it gets loaded at and the x1000 is the size of the section, they are seperate in that one specifies properties of the process after its loaded and one specifies properties of the EXE. ...............

### Launching Firefox 0.9

Raj Shekhar (rajshekhar from hotpop.com)

Hello

I had used the following script for launching Firefox (earlier named firebird) (See my article Mozilla Firebird - A review (http://linuxgazette.net/issue97/shekhar.html) to see how you can use this).

    #!/bin/sh
    /usr/lib/mozilla-firebird/MozillaFirebird  -remote "openURL('$@', new-tab)" ||
    exec /usr/lib/mozilla-firebird/MozillaFirebird "$@";

However, with the 0.9 release (the latest one, which is even more slick and fast), this stopped working. Instead of launching the new tab, the Profile Manager UI pops up. This was reported as bug http://bugzilla.mozilla.org/show_bug.cgi?id=246168 in the Mozilla Bugzilla.

The reason for this is the change in the format of calling the remote command. The new way of doing this is:

    firefox -a firefox -remote openURL(<url>,new-tab)

Note that there should be no space between the the comma and the command after the url (new-tab in this case, you can also use new-window to start a new window) or you get a "Error: Failed to send command: 509 internal error".

So the script to launch firefox becomes

    #!/bin/sh
    /usr/lib/firefox/firefox -a firefox  -remote "openURL("$@",new-tab)" ||
    exec /usr/lib/firefox/firefox "$@";

> [Ben] I just had the same problem with the new Mozilla upgrade; the trouble is in the brain-dead URL parser at the end of the wrapper script. Although I fixed it, it very quickly became useless: I downloaded Mozilla 1.7 from http://mozilla.org, and its wrapper script works fine.

> Oh, and since you brought this issue up: just yesterday, I was reading about Mozilla's "remote" methods in order to fix the above problem - and still forgot, completely, that there's a "native" way to test if Mozilla is running or not.

> Wonder if I can trade this brain in for a newer model?

>     mozilla -remote "ping()" 2>/dev/null && mozilla -remote "openurl($1,new-window)" || mozilla $1

### Checkinstall

Neil Youngman (neil.youngman from wirefast.com)

This one courtesy of LWN. You want to build a package from the latest source, but have it managed by your favourite package manager. Checkinstall claims to automate the process for Debian (presumably any system using dpkg?), Slackware and RPM based systems. I gotta have it

http://checkinstall.izto.org

Publication by LWN is hereby permitted

> [Thomas] I have used both, and my personal preference would be the use of stow over checkinstall.

> Typing stow as a keyword into the search box at freshmeat.net also reveals some interesting competitors, and a front end for stow.

> Also, Allan Peda had an article on this topic in Issue 75, Simple Package Management With Stow -- Heather

### Periodic table of the operators

Jimmy O'Regan (The LG Answer Gang)

One for Ben: a periodic table of Perl 6's operators.

http://www.ozonehouse.com/mark/blog/code/PeriodicTable.html

> [Ben] "Today, we're going to learn about one of the varieties of serious mental disturbance. Here, for example, is a distinctly pathological case..."

> It's got some amount of cuteness to it, but I think you'd have to be a bit obsessed to actually do the thing up. Reminds me of an excellent model of the Titanic, about 8' long IIRC, built out of matchsticks - all the way down to the deck chairs and the oars in the lifeboats - that I saw at an exhibition of artworks done by mental patients. Much of the work was incredible, brilliant, and unrepeatable in many cases - and the stories (such as that of a woman who made these shockingly beautiful patterns from threads she'd pluck out of rags... and almost all of whose work had been thrown away because it was just "trash" made by "crazy Nancy") would break your heart.

### "Remote" authentication with PHP

Lew Pitcher (lpitcher from sympatico.ca)

Hi Guys

Here's another contribution. Hopefully, you can publish it as a 'two cent tip'

As part of a PHP web app I'm playing with, I needed to authenticate the web client user with a remote system. Unfortunately, this system is a mainframe and setting up a web-enabled authentication product on it is somewhat timeconsuming and requires a lot of administrivia. I wanted to avoid all that, so I had to come up with another way to authenticate web users remotely.

The one TCP/IP networked app our mainframe has available is FTP. Now, the FTP protocol implements security processes with the 'USER' and 'PASS'word commands, and our host security people have ensured that the host FTP server requires these two functions. In our case, the 'USER' and 'PASS' functions on the server interface with the ACF2 security system to validate that the given userid and password combination are correct, and will not let an FTP connection in if they aren't.

I use this little tidbit of information to let me authenticate web users of my Linux box by forcing their web browsers to pop up the Authentication panel, and sending their entered userid and password information to the host in an FTP 'USER' and 'PASS'word command sequence. If the host's FTP rejects the sequence, then the user isn't authorized, but if the host's FTP accepts the sequence, then the user is valid to the host. In either case, I don't actually transfer files over the FTP link; I simply close it unused. I only need it for the authentication.

Neat or what?

Here's an example PHP script that demonstrates the process. It needs an ftp server in order to work, and is (for demonstration purposes) set up to talk to the ftp server at localhost...

    <?php

      /*
      ** LoginPrompt() sends headers and html with the intent of
      ** inducing the web-browser to display it's built-in userid/password
      ** prompt.
      ** It sends a WWW-Authenticate header to give the authentication specs,
      **          a HTTP 401 on the current page requested by the browser, and
      **          a dummy HTML page to be displayed if the user cancels the
      **            login prompt
      ** It then exits, causing php to terminate the current transaction
      ** without further output
      */
      function LoginPrompt($URL)
      {
        /* force the login popup to show up */
        Header("WWW-Authenticate: Basic realm=\"System Login\"");
        Header("HTTP/1.0 401 Unauthorized");

        /* if the user hits Cancel, send him to a place he cant hurt us from */
        echo "<meta HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL=$URL\">";

        exit;
      }

      $userid = $_SERVER['PHP_AUTH_USER'];
      $passwd = $_SERVER['PHP_AUTH_PW'];

      $validuser = "no";

      if ($userid && $passwd)
      {

        /* connect to FTP server, see if it accepts the given userid & password */
        $conn = ftp_connect("localhost") or die("Cant connect");
        if (@ftp_login($conn,$userid,$passwd))  $validuser = "yes";
        ftp_close($conn);

        if ($validuser == "no") /* bad user - try the login again */
          LoginPrompt("http://www.php.net/manual/en/features.http-auth.php");
      }
      else /* first time into this page - force the 1st login prompt */
        LoginPrompt("http://www.php.net/manual/en/features.http-auth.php");

      phpinfo();

    ?>

### Xserver spits Xclient hosts

Chris Christensen (Chris.Christensen from aspenresearch.com)

> This is in reply to:

> http://linuxgazette.net/103/lg_tips.html#tips.4 -- Thomas Adam

Regarding Xserver spits multiple windows to foreign Xclient:

> I have no knowledge of Exceed. I use cygwin to connect to unix/linux boxes. You have to configure/install cygwin with X (and ssh, if you're going to use that, and why not?). Crucial step is to start an xwdm (blackbox I think, whatever is available on Cygwin) first, and start linux x-apps from inside the xwdm (start with an xterm!). The full screen x-windows display manager is actually running locally, while the windowed apps are in the dm.

Chris C

### Fun game in 64 LoC

Ben Okopnik (LG Technical Editor)

Ambassador of Pain - quite entertaining for 64 lines of code. (That's in C, mind you; if it was Perl, I'd be expecting Quake3, including the PAK file.

http://raffi.at/view/code/aop

### Grokdoc

Jimmy O'Regan (The LG Answer Gang)

A while back, Groklaw mentioned that they wanted to have a Linux usability test, and technical documentation written for new users. They've started a wiki (based on the Wikipedia software) to do this:

> http://www.grokdoc.net/index.php/Main_Page

This page edited and maintained by the Editors of Linux Gazette
HTML script maintained by Heather Stern of Starshine Technical Services, http://www.starshine.org/

Copyright © 2004, . Released under the Open Publication license

Published in Issue 104 of Linux Gazette, July 2004

<-- prev | next -->