#!/usr/bin/perl
# $Id: last_section,v 1.0 92/07/31 17:39:39 cap Exp $
# This program prints information about the last segment section to
# appear in the input executable. If the link run that made this executable
# was normal, the last segment will be located at the top of the program's 
# address space, so any address after the end of this section are free.
# The output of this program is code to execute in a /bin/sh script. 
# The code consists of variable assignments, such as
#
#     segname=__TIFF; sectname=miniTrash.tiff; endaddr=0xbf520
#
die "usage: last_section input\n" if $#ARGV != 0;
$input = $ARGV[0];

$command = "otool -l $input";
open(INPUT, "$command |") || die "Can't run \"$command\"\n";

# set the input separator to the string appearing after the end of
# the search string in the if
$/ = "    offset";

while (<INPUT>) {
    if (/  sectname (.*)\n   segname (.*)\n      addr 0x(.*)\n      size 0x(.*)\n/) {
	$sectname = $1;
	$segname = $2;
	$addr = $3;
	$size = $4;
    }
}
# Memory address of new data. File offset is probably irrelevant.
$endaddr = hex($addr) + hex($size);
close(INPUT);

printf "\$segname=\"%s\"; \$sectname=\"%s\"; \$endaddr=0x%x\n", 
	$segname, $sectname, $endaddr;
