001    // This file is part of the program FRYSK.
002    //
003    // Copyright 2006, 2007, 2008 Red Hat Inc.
004    //
005    // FRYSK is free software; you can redistribute it and/or modify it
006    // under the terms of the GNU General Public License as published by
007    // the Free Software Foundation; version 2 of the License.
008    //
009    // FRYSK is distributed in the hope that it will be useful, but
010    // WITHOUT ANY WARRANTY; without even the implied warranty of
011    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012    // General Public License for more details.
013    // 
014    // You should have received a copy of the GNU General Public License
015    // along with FRYSK; if not, write to the Free Software Foundation,
016    // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
017    // 
018    // In addition, as a special exception, Red Hat, Inc. gives You the
019    // additional right to link the code of FRYSK with code not covered
020    // under the GNU General Public License ("Non-GPL Code") and to
021    // distribute linked combinations including the two, subject to the
022    // limitations in this paragraph. Non-GPL Code permitted under this
023    // exception must only link to the code of FRYSK through those well
024    // defined interfaces identified in the file named EXCEPTION found in
025    // the source code files (the "Approved Interfaces"). The files of
026    // Non-GPL Code may instantiate templates or use macros or inline
027    // functions from the Approved Interfaces without causing the
028    // resulting work to be covered by the GNU General Public
029    // License. Only Red Hat, Inc. may make changes or additions to the
030    // list of Approved Interfaces. You must obey the GNU General Public
031    // License in all respects for all of the FRYSK code and other code
032    // used in conjunction with FRYSK except the Non-GPL Code covered by
033    // this exception. If you modify this file, you may extend this
034    // exception to your version of the file, but you are not obligated to
035    // do so. If you do not wish to provide this exception without
036    // modification, you must delete this exception statement from your
037    // version and license this file solely under the GPL without
038    // exception.
039    
040    package lib.dwfl;
041    
042    import inua.eio.ByteBuffer;
043    import inua.eio.ArrayByteBuffer;
044    import inua.eio.ByteOrder;
045    
046    /**
047     * Java Representation of the the NT_AUXV notes secion found in core
048     * files
049     **/
050    public class ElfPrAuxv extends ElfNhdr.ElfNoteSectionEntry {
051    
052        private byte[] noteData;
053        private ByteBuffer noteBuffer;
054    
055        public ElfPrAuxv(int length, int wordSize, ByteOrder byteOrder) {
056            noteData = new byte[length * 2 * wordSize];
057            noteBuffer = new ArrayByteBuffer(noteData);
058            noteBuffer.order(byteOrder);
059            noteBuffer.wordSize(wordSize);
060        }
061    
062        private ElfPrAuxv(Elf elf, byte[] noteData) {
063            this.noteData = noteData;
064            noteBuffer = new ArrayByteBuffer(noteData);
065            ElfEHeader header = elf.getEHeader();
066            noteBuffer.order(header.getByteOrder());
067            switch (header.machine) {
068            case ElfEMachine.EM_386:
069            case ElfEMachine.EM_PPC:
070                noteBuffer.wordSize(4);
071                break;
072            case ElfEMachine.EM_X86_64:
073            case ElfEMachine.EM_PPC64:
074                noteBuffer.wordSize(8);
075                break;
076            default:
077                return;
078            }
079        }
080    
081        public static ElfPrAuxv decode(ElfData noteData) {
082            final byte data[] = getNoteData(noteData);
083            ElfPrAuxv auxData = new ElfPrAuxv(noteData.getParent(), data);
084            return auxData;
085        }
086    
087        /**
088         *
089         * Return auxv data, in raw form
090         *
091         * @return: buffer - byte[] array containing buffer
092         *
093         */
094        public ByteBuffer getByteBuffer() {
095            return noteBuffer;
096        }
097    
098        public byte[] getByteArray() {
099            return noteData;
100        }
101    
102        /**
103         * Returns the entry size associated with this notes buffer.
104         */
105        public long getEntrySize() {
106            return noteData.length;
107        }
108    
109        /**
110         * This is called when the notes section is filled.  Fill the
111         * passed buffer with your own data, starting at startAddress
112         */
113        public long fillMemRegion(byte[] buffer, long startAddress) {
114            System.arraycopy(noteData, 0, buffer, (int)startAddress,
115                             noteData.length);
116            return noteData.length;
117        }
118    
119        private native static byte[] getNoteData(ElfData data);
120    }